如何设置 Selenium Python WebDriver 默认超时? [英] How to set Selenium Python WebDriver default timeout?
问题描述
试图找到一种在 Selenium Python WebDriver 中为命令执行延迟设置最大时间限制的好方法.理想情况下,类似于:
Trying to find a good way to set a maximum time limit for command execution latency in Selenium Python WebDriver. Ideally, something like:
my_driver = get_my_driver()
my_driver.set_timeout(30) # seconds
my_driver.get('http://www.example.com') # stops / throws exception when time is over 30 seconds
会起作用.我找到了 .implicitly_wait(30)
,但我不确定它是否会导致所需的行为.
would work. I have found .implicitly_wait(30)
, but I'm not sure if it results in the desired behavior.
如果它有用,我们专门使用 Firefox 的 WebDriver.
In case it is useful, we are specifically using the WebDriver for Firefox.
编辑
根据@amey 的回答,这可能有用:
As per @amey's answer, this might be useful:
ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")
但是,我不清楚隐式等待是否同时适用于 get
(这是所需的功能)和 find_element_by_id
.
However, it is not clear to me whether the implicit wait applies both to get
(which is the desired functionality) and to find_element_by_id
.
非常感谢!
推荐答案
在python中,创建页面加载超时的方法是:
In python, the method to create a timeout for a page to load is:
Firefox 和 Chromedriver:
driver.set_page_load_timeout(30)
其他::
driver.implicitly_wait(30)
只要页面加载时间超过 30 秒,就会抛出 TimeoutException
.
This will throw a TimeoutException
whenever the page load takes more than 30 seconds.
这篇关于如何设置 Selenium Python WebDriver 默认超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!