How to set Selenium Python WebDriver default timeout?

后端 未结 4 1076
星月不相逢
星月不相逢 2020-11-27 04:15

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_m         


        
相关标签:
4条回答
  • 2020-11-27 04:39

    In python, the method to create a timeout for a page to load is:

    Firefox and 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.

    0 讨论(0)
  • 2020-11-27 04:46

    Information about Explicit and Implicit waits can be found here.

    UPDATE

    In java I see this, based of this :

    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.

    0 讨论(0)
  • 2020-11-27 04:46

    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

    0 讨论(0)
  • 2020-11-27 04:47

    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/")
    
    0 讨论(0)
提交回复
热议问题