首页 > 解决方案 > Python & Selenium :: 由于网页未完全加载/显示,无法定位元素

问题描述

我正在编写一个 python-selenium 脚本来使用 mail.google.com 自动发送电子邮件。

HTML页面:

<textarea rows="1" id=":125" class="vO" name="to" spellcheck="false" autocomplete="false" autocapitalize="off" autocorrect="off" tabindex="1" dir="ltr" aria-label="To" role="combobox" aria-autocomplete="list" style="width: 391px;"></textarea>

我尝试了什么:

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Firefox()
#Open URL
driver.get("https://mail.google.com/") 
#Enter username
driver.find_element_by_id("identifierId").send_keys("test@gmail.com") 
#click next button in username page
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div/div[1]/div/content/span").click()
sleep(4) 
#Enter password
password_xpath="/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/content/form/div[1]/div/div[1]/div/div[1]/input"
driver.find_element_by_xpath(password_xpath).send_keys("test")
#click next button in password page
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div/div[1]/div/content/span").click()
sleep(2) 
#Compose new mail
driver.get("https://mail.google.com/mail/u/0/#inbox?compose=new")
sleep(6) 
#Type Recipient's email address
#The following code is to enter the Recipient's email address in Compose Window
#----------------------------------------------------------------------------
driver.find_element_by_id(":125").send_keys("abc@gmail.com")
#driver.find_element_by_name("to").send_keys("abc@gmail.com")
#driver.find_element_by_xpath("//*[@id=':125']").send_keys("abc@gmail.com")
#driver.find_element_by_link_text("To").send_keys("abc@gmail.com")
#driver.find_element_by_partial_link_text("To").send_keys("abc@gmail.com")
#driver.find_element_by_link_text("Recipients").send_keys("abc@gmail.com")
#driver.find_element_by_partial_link_text("Recipients").send_keys("abc@gmail.com")
#driver.find_element_by_tag_name("textarea").send_keys("abc@gmail.com")
#driver.find_element_by_class_name("v0").send_keys("abc@gmail.com")
#driver.find_element_by_class_name("wA").send_keys("abc@gmail.com")
#driver.find_element_by_css_selector("#\:125").send_keys("abc@gmail.com")
#----------------------------------------------------------------------------
#Type Subject
#driver.find_element_by_id(":125").send_keys("abc@gmail.com")
#Type Content
#driver.find_element_by_id(":125").send_keys("abc@gmail.com")
#Click Send button
#driver.find_element_by_id(":15r").click()

问题:

加载和显示 Compose 窗口需要一些时间。因此它显示“无法定位元素”,因为由于 Internet 连接速度慢而未显示撰写窗口。

输出:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id=":125"]
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="to"]
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id=':125']

我从这里使用了以下代码

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, ":125"))).send_keys("abc")

但它显示以下错误:

Traceback (most recent call last):
  File "sendmail2.py", line 24, in <module>
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, ":125"))).send_keys("abc")
  File "/home/dipankar/.local/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

编辑:(这是完整的 HTML 代码)

<div class="wO nr l1"><input class="wA" tabindex="-1" aria-hidden="true"><textarea rows="1" id=":zo" class="vO" name="to" spellcheck="false" autocomplete="false" autocapitalize="off" autocorrect="off" tabindex="1" dir="ltr" aria-label="To" role="combobox" aria-autocomplete="list" style="width: 391px;"></textarea><div class="aA6"><span><div style="width: 1px; height: 1px; position: absolute;" tabindex="1"></div><div style="width: 1px; height: 1px; position: absolute;" tabindex="1"></div><span><span id=":14u" class="aB gQ pE" role="link" tabindex="1" data-tooltip="Add Cc Recipients ‪(Ctrl-Shift-C)‬" aria-label="Add Cc Recipients ‪(Ctrl-Shift-C)‬" style="-moz-user-select: none;">Cc</span><span id=":14r" class="aB  gQ pB" role="link" tabindex="1" data-tooltip="Add Bcc Recipients ‪(Ctrl-Shift-B)‬" aria-label="Add Bcc Recipients ‪(Ctrl-Shift-B)‬" style="-moz-user-select: none;">Bcc</span><span id=":10m" role="button" tabindex="1" aria-hidden="false" class="bcV" style="visibility: visible; display: none;" data-tooltip="Some recipients use services that don't support encryption (click for details)" aria-label="Some recipients use services that don't support encryption (click for details)"></span></span><div style="width: 1px; height: 1px; position: absolute;" tabindex="1"></div></span></div></div>

编辑 2:临时解决方案: [以下代码有效]

(这里我增加了等待时间,因为网络速度很低。)

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver=webdriver.Firefox()
#Open URL
driver.get("https://mail.google.com/") 
#Enter username
driver.find_element_by_id("identifierId").send_keys("sender@gmail.com") 
#click next button in username page
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div/div[1]/div/content/span").click()
sleep(4) 
#Enter password
password_xpath="/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/content/form/div[1]/div/div[1]/div/div[1]/input"
driver.find_element_by_xpath(password_xpath).send_keys("test")
#click next button in password page
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div/div[1]/div/content/span").click()
sleep(2) 
#Compose new mail
compose_button_xpath="/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div/div[1]/div/div"
driver.find_element_by_xpath(compose_button_xpath).click()
sleep(10)
#Wait 20 seconds for compose window. It depends upon network speed
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, ":zo")))
#The following code is to enter the Recipient's email address in Compose Window
driver.find_element_by_xpath("//*[@id=':125']").send_keys("receiver@gmail.com,")
#Type Subject
driver.find_element_by_name("subjectbox").send_keys("Subject123")
#Type Content
driver.find_element_by_xpath("//*[@id=':12s']").send_keys("Content")
#Click Send button
driver.find_element_by_xpath("//*[@id=':11d']").click()

标签: pythonselenium

解决方案


您可以在 Xpath 下面使用

driver.find_element_by_xpath("//textarea[@name='to']").send_keys("abc@gmail.com")

推荐阅读