首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python & Selenium ::无法定位元素,因为网页未完全加载/显示

Python & Selenium ::无法定位元素,因为网页未完全加载/显示
EN

Stack Overflow用户
提问于 2018-06-22 01:41:34
回答 2查看 833关注 0票数 0

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

网页:

代码语言:javascript
复制
<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>

我尝试过的东西:

代码语言:javascript
复制
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()

问题:

加载和显示撰写窗口需要一些时间。因此它会显示"Unable to locate element“,因为由于Internet连接速度较慢,撰写窗口不会显示。

输出:

代码语言:javascript
复制
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']

我使用了here中的以下代码

代码语言:javascript
复制
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, ":125"))).send_keys("abc")

但它显示了以下错误:

代码语言:javascript
复制
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:(这里是完整的代码)

代码语言:javascript
复制
<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>

EDIT 2:临时解决方案:以下代码有效

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

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2018-06-22 02:27:53

您可以使用下面的Xpath

代码语言:javascript
复制
driver.find_element_by_xpath("//textarea[@name='to']").send_keys("abc@gmail.com")
票数 0
EN

Stack Overflow用户

发布于 2018-06-22 02:13:53

你可以实现一个等待某个元素出现的函数。可能是这样的:

代码语言:javascript
复制
from selenium.common.exceptions import NoSuchElementException

def wait_for_element(driver, id_selector, timeout=30):
    start = time.time()

    while time.time() - start < timeout:
        try:
            return driver.find_element_by_id(id_selector)
        except NoSuchElementException:
            time.sleep(0.1)
            continue
    raise NoSuchElementException

显然,您不能等到无穷大,因此应该有一个超时。然后在你的代码中:

代码语言:javascript
复制
my_input = wait_for_element(driver, ":125")
my_input.send_keys("abc@gmail.com")
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50974639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档