python - 如何使用python减少在selenium webdriver中找不到元素时的等待时间

标签 python selenium selenium-webdriver

<分区>

这个问题有些不同。
在我的程序中,我有超过 9 个预期的断言。因此,如果找不到元素,我必须减少等待时间。

for i in range(len(10)):
    try:
        i.click()
        driver.find_element_by_name("XXX").send_keys("XXXX")
    except Exception,e:
        print "this is an expected assertion so as to continue the program further iteration"

在上面的程序中,由于 Unable to find element 断言它花费了很多时间..
我怎样才能减少等待时间...

最佳答案

import selenium.webdriver.support.ui as ui 添加到您的 header 。

WebDriverWait 对象将等待直到找到您想要的内容:

try:
    timeout = 10
    wait = ui.WebDriverWait(driver, timeout)
    wait.until(
        lambda driver : driver.find_element_by_name("XXX").send_keys("XXXX")
    )
except TimeoutException:
    # timeout

编辑:

WebDriver 有一个默认的 60 秒内部超时(这是 ruby​​ 文档,但对于 python 应该是相同的):

Internally, WebDriver uses HTTP to communicate with a lot of the drivers (the JsonWireProtocol). By default, Net::HTTP from Ruby's standard library is used, which has a default timeout of 60 seconds. If you call Driver#get on a page that takes more than 60 seconds to load, you'll see a TimeoutError raised from Net::HTTP.

尝试像这样重置超时:

driver.set_page_load_timeout(0)
driver.set_script_timeout(0)

文档:set_page_load_timeout , set_script_timeout

关于python - 如何使用python减少在selenium webdriver中找不到元素时的等待时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20627226/

相关文章:

python - python新手: pyodconverter with Apache OpenOffice 4 on Windows

javascript - Jmeter WebDriver Sampler 如何加载 Firefox 配置文件

python - Selenium 无法打开指定的 URL 并显示数据 :,

testing - 如何读取 cucumber 项目中的excel文件?

java - 无法通过 selenium 中的 htmlunit 驱动程序运行脚本

python - (PyQt 和 PySide)QFileDialog.getExistingDirectory 在 Windows 上不起作用

Python tkinter : issues resizing frames

python - 是否有一个简洁的 emacs lisp 等效于 Python 的 [n :m] list slices?

java - Selenium 3.14.0+ 升级 selenium 后 Actions.movetoElement 出现 ClassCast 异常

java - 除非在 @Test 中声明驱动程序实例,否则无法使 Actions.class 方法工作