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

时间:2018-06-21 17:41:34

标签: 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()

问题:

加载并显示“撰写”窗口需要花费一些时间。由于互联网连接速度缓慢,因此未显示撰写窗口,因此它显示“无法定位元素”。

输出:

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

中的以下代码
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()

2 个答案:

答案 0 :(得分:0)

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

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

很显然,您不能等到无限远,因此应该有一个超时时间。然后在您的代码中:

my_input = wait_for_element(driver, ":125")
my_input.send_keys("abc@gmail.com")

答案 1 :(得分:0)

您可以在Xpath下使用

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