【问题标题】:Selenium Timeout Exception python硒超时异常python
【发布时间】:2020-04-29 13:51:03
【问题描述】:

所以,我正在使用 selenium 创建一个注册机。当此页面加载到 https://mail.protonmail.com/create/new?language=en 时,它无法通过 usernameid/xpath 找到元素。另一方面,它能够找到 password,passwordc 元素。我尝试使用 WebDriverWait 功能,但它给出了 超时错误。尝试了很多东西,但这东西仍然给我错误。如果可能的话,建议在最后一页上找到 usernameelement 或完美的 WebDriverWait 代码。下面是我的代码

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

url = 'https://protonmail.com/'
driver = webdriver.Chrome('C://Users/AAA/Desktop/chromedriver.exe')
driver.get(url)

driver.find_element_by_xpath('//*[@id="bs-example-navbar-collapse-1"]/ul/li[8]/a').click()
time.sleep(2)
driver.find_element_by_xpath('//*[@id="signup-plans"]/div[5]/div[1]/div[1]/div/div[1]/h4').click()
time.sleep(1)
driver.find_element_by_id('freePlan').click()
time.sleep(1)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "username")))
driver.find_element_by_xpath('//*[@id="username"]').send_keys('santaking44455')
time.sleep(1)
driver.find_element_by_id('password').send_keys('25J8e5b8')
time.sleep(1)
driver.find_element_by_id('passwordc').send_keys('25J8e5b8')```

【问题讨论】:

  • 不要这样使用睡眠。 Selenium 具有您需要能够正确处理页面加载延迟所需的所有功能,而无需诉诸旧的“随机间隔睡眠”技巧。

标签: python python-3.x selenium selenium-webdriver


【解决方案1】:

您找不到它,因为它位于 html 源代码中较高的 iframe 标记中。先切换到iframe,然后你应该可以和元素交互了。

iframe=driver.find_element_by_xpath('//*[@title="Registration form"]')
driver.switch_to.frame(iframe)
driver.find_element_by_xpath('//*[@id="username"]').send_keys('santaking44455')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2021-08-20
    • 2021-08-23
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多