如何设置 Selenium Python WebDriver 默认超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17533024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Selenium Python WebDriver default timeout?
提问by Juan Carlos Coto
Trying to find a good way to set a maximum time limit for command execution latency in Selenium Python WebDriver. Ideally, something like:
试图找到一种在 Selenium Python WebDriver 中为命令执行延迟设置最大时间限制的好方法。理想情况下,类似于:
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
would work. I have found .implicitly_wait(30)
, but I'm not sure if it results in the desired behavior.
会工作。我找到了.implicitly_wait(30)
,但我不确定它是否会导致所需的行为。
In case it is useful, we are specifically using the WebDriver for Firefox.
如果它有用,我们专门使用 Firefox 的 WebDriver。
EDIT
编辑
As per @amey's answer, this might be useful:
根据@amey 的回答,这可能有用:
ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")
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
.
但是,我不清楚隐式等待是否同时适用于get
(这是所需的功能)和find_element_by_id
.
Thanks very much!
非常感谢!
采纳答案by Juan Carlos Coto
In python, the method to create a timeout for a page to load is:
在python中,为页面加载创建超时的方法是:
Firefox and Chromedriver:
Firefox 和 Chromedriver:
driver.set_page_load_timeout(30)
Other::
其他::
driver.implicitly_wait(30)
This will throw a TimeoutException
whenever the page load takes more than 30 seconds.
TimeoutException
只要页面加载时间超过 30 秒,就会抛出一个。
回答by Amey
Information about Explicit and Implicit waits can be found here.
可以在此处找到有关显式和隐式等待的信息。
UPDATE
更新
In java I see this, based of this:
在java中我看到了这一点,基于这个:
WebDriver.Timeouts pageLoadTimeout(long time,
java.util.concurrent.TimeUnit unit)
Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
Parameters:
time - The timeout value.
unit - The unit of time.
Not sure of the python equivalent.
不确定 python 等效项。
回答by Nima Soroush
The best way is to set preference:
最好的方法是设置首选项:
fp = webdriver.FirefoxProfile()
fp.set_preference("http.response.timeout", 5)
fp.set_preference("dom.max_script_run_time", 5)
driver = webdriver.Firefox(firefox_profile=fp)
driver.get("http://www.google.com/")
回答by Roni davelman
My solution was to run an asynchronous thread alongside the browser load event, and have it close the browser and re-call the load function if there was a timeout.
我的解决方案是在浏览器加载事件旁边运行一个异步线程,并让它关闭浏览器并在超时时重新调用加载函数。
#Thread
def f():
loadStatus = true
print "f started"
time.sleep(90)
print "f finished"
if loadStatus is true:
print "timeout"
browser.close()
call()
#Function to load
def call():
try:
threading.Thread(target=f).start()
browser.get("http://website.com")
browser.delete_all_cookies()
loadStatus = false
except:
print "Connection Error"
browser.close()
call()
Call() is a function which just
Call() 是一个函数,它只是