Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置Selenium Python WebDriver默认超时?_Python_Firefox_Selenium_Timeout_Selenium Webdriver - Fatal编程技术网

如何设置Selenium Python WebDriver默认超时?

如何设置Selenium Python WebDriver默认超时?,python,firefox,selenium,timeout,selenium-webdriver,Python,Firefox,Selenium,Timeout,Selenium Webdriver,试图找到一种在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 会有用的。我已经找到了.implicitl

试图找到一种在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
会有用的。我已经找到了
.implicitly\u wait(30)
,但我不确定它是否会产生所需的行为

如果它有用,我们将专门使用Firefox的WebDriver

编辑

根据@amey的回答,这可能很有用:

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\u element\u by\u id


非常感谢

可以找到有关显式和隐式等待的信息

更新

在java中,我看到这一点,基于:


不确定python等价物。

在python中,为要加载的页面创建超时的方法是:

Firefox和Chromedriver

driver.set_page_load_timeout(30)
driver.implicitly_wait(30)
其他:

driver.set_page_load_timeout(30)
driver.implicitly_wait(30)

每当页面加载时间超过30秒时,这将抛出一个
TimeoutException

最好的方法是设置首选项:

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/")

我的解决方案是在浏览器加载事件旁边运行一个异步线程,让它关闭浏览器,并在出现超时时重新调用加载函数

#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()是一个函数,我刚刚看了源代码。对于python绑定来说,它是模糊的。但对于C#,
隐式等待
仅适用于
FindElement/FindElements
(与Java相同)。资料来源:谢谢。如果您感兴趣,请参阅下面的答案。@ivan_bilan:如果您指的是
验证
,则不返回任何
dom。max\u script\u run\u time
为执行javascript设置超时。这不是一个完整的页面加载超时。这在Python 3.7.4、selenium 3.141.0和gecko 0.26.0中失败。
set_page_load_timeout
在chromedriver 75 for meThis中工作。这在Python 3.7.4、selenium 3.141.0和gecko 0.26.0中失败。如果网站脱机,您的递归缺少中断条件。如果需要,在成功时使用带有
中断的无限循环。