Python Selenium WebDriverWait 问题

Python Selenium WebDriverWait issue

提问人:Rahul S L 提问时间:6/10/2021 更新时间:6/10/2021 访问量:644

问:

我正在尝试使用 WebDriverWait 来检查我是否可以看到预期的仪表板 url 已加载,并且我还检查是否存在强制徽标。我有下面的相关代码,

if WebDriverWait (driver, 30).until(EC.url_to_be(<expected_dashboard_url>) and driver.find_elements_by_id(<logo_id>):
    print("Successful")

我看到仪表板 url 在浏览器上正确加载,页面上也有预期的强制徽标,但在我的控制台上我没有看到“成功”消息,而是继续等待并导致 TimeoutException。

注意:所有必要的导入都已完成,并且 webdriver 已正确实例化。

python-3.x selenium-webdriver webdriverwait chrome-web-driver

评论

0赞 Rahul S L 6/10/2021
令人困惑的部分是,如果我尝试运行脚本 5 次,1 次我看到成功,其余 4 次我看到 TimeoutException,即使页面加载正确。

答:

0赞 Hüseyin Avcı 6/10/2021 #1

如果我正确理解了问题,你可以试试这个;

browser.get("url")
delay = 5
try:
    element = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'id')))
except TimeoutException:
    print "not loaded"

我之前遇到过这个问题,并用此链接中的解决方案解决了它。如果这不起作用,你可以看看;

等到页面加载到 Selenium WebDriver for Python