selenium study notes 11 - driver.get (url) page load time is too long

In the process of the implementation of automated test case, because the network is slow or find other causes when driver.get (url), the page has been loaded, the page is not loaded would not have to continue with the following action, but in fact need to operate elements already loaded out.

Solution

The first step: Use set_script_timeout () Set the maximum wait time.

Step two: the maximum wait time if the page is still not finished loading, execution js code, driver.execute_script ( "window.stop ()" ) to stop the page loads, perform the following steps automated testing.
code show as below:

driver = self.driver
  1. Set the maximum waiting time is 10 seconds.
  2. If 10 seconds is not loaded, print "time out after 10 seconds when loading page!", Then stop loading directly perform the following test procedures.
# Set the maximum page load time 
        driver.set_page_load_timeout (10 )
         the try : 
            driver.get (self.base_url) 
        the except TimeoutException:
             Print  ' ! ! ! ! ! ! time out after 10 seconds when loading page ! ! ! ! ! ! ' 
            # When the page is loaded more than the set time, to STOP by js, to perform subsequent actions 
            driver.execute_script ( " window.stop () " )
  1. Set the maximum waiting time is 10 seconds.
  2. If 10 seconds is not loaded, print "time out after 10 seconds when loading page!", Then stop loading directly perform the following test procedures.
    DEF page_loading_timeout (Driver, URL, Time):
         '' ' 
        : param Driver: parameter 1, passed browser object 
        : param url: parameter 2, the incoming URL 
        : param Time: 3 parameters, set the timeout in seconds 
        : return: 
        '' ' 
        driver.set_page_load_timeout (Time) 
        the try : 
            driver.get (URL) 
        the except :
             Print  " !!!!!! Time% S OUT After loading page !!!!!! when seconds The " % Time
             # when the page load time exceeds the set time, to STOP by js, to perform subsequent actions 
            driver.execute_script ( " window.stop () " )

     

Guess you like

Origin www.cnblogs.com/stvadv/p/11667939.html