这个问题有些不同。
在我的程序中,我有超过 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