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

# Two simultaneous job setting 
# achieve the effect of: loading stop state, the next operation code 
driver.set_page_load_timeout (10 ) 
driver.set_script_timeout ( 10)   # Both settings are valid for 
the try : 
    driver.get ( " HTTPS: //shopee.co.id/search?keyword=jam%20tangan&page= " + STR (Page) + " & the sortBy = " + rankBy)
 the except : 
    driver.execute_script ( ' window.stop () ' ) 
------ ---------- 
Disclaimer: this article is the original article CSDN bloggers "cool_soup29", and follow CC 4.0 BY- SA copyright agreement, reproduced, please attach the original source link and this statement. 
Original link: https://blog.csdn.net/cool_soup29/article/details/88657643

 

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
         # 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.
    # - * - Coding: UTF-8 - * - 
    # @time: 2018/8/6 11:12 
    # @author: North Galaxy 
    # @email: [email protected] 
    # @Introduction: XXX 
    
    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/11653381.html