How do I set the wait time for Python selenium

  • 2020-05-10 18:26:56
  • OfStack

The WebDriver test method we described earlier is based on web pages. The previous examples are simple web pages to operate, may not experience the page loading process, but in the actual application process, web page loading is to consume 1 fixed time. Your script is already running, but the element you want to locate is not yet loaded, and an error is reported that the element cannot be found. Obviously, a script that does not take load time into account is not a successful script. Today we're going to show you how to set the wait time.

Three ways to wait

time.sleep(n)

Force n seconds. The functions of Python itself, included in the time package, need to be imported into the time package before use. We used to use this kind of wait in our previous examples so that you can see the results of script execution. This wait method is 10 minutes clumsy, no matter how the page loads, you have to wait for n seconds, so 10 minutes is not smart.

implicitly_wait(n)

The maximum waiting time is n seconds. If the page load is completed within n seconds, the waiting will be terminated prematurely. WebDriver provide wait for method, also known as the implicit waiting, smarter than mandatory waiting 1 point, but if the page itself contains a large such as video files, even if we need to locate elements from the beginning has been loaded, but still want to wait for all documents after loading, the script will continue to execute, is still has some drawbacks.

WebDriverWait(n)

The maximum waiting time is n seconds, with n seconds checking the existence of the element to be located at intervals of 1 period, and if so, ending the waiting earlier. Also provided by WebDriver is the wait method, also known as the explicit wait, which is smarter than the implicit wait. It ignores the loading of the entire page and ends the wait as soon as the required elements exist.

The instance

Forced wait was used in the previous example, so let's take a look at one of the two waiting methods provided by WebDriver

The recessive waiting

In fact, implicit wait was also used in the previous introduction, but the method used was not specifically mentioned. We will open the home page of netease this time, which is a portal website loaded with a lot of content. According to the network speed, it will take about 10 seconds to finish loading by visual inspection. We will set the waiting time to 60 seconds, and then calculate how long it will take from opening the page to clicking the "open class" button in the navigation bar of the page.


# coding = utf-8
from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.implicitly_wait(60) # Hidden waiting time 60 seconds 
time_start = time.time() # Record the time it takes to open the page 
driver.get('https://www.163.com/')
driver.find_element_by_id('js_love_url').click()
time_end = time.time() # Record the time after the button is clicked 
print('access time is : ', time_end - time_start) # Print time difference, that is, the actual consumption of time 
time.sleep(2) # Mandatory waiting 2 Second, to see that we did open the open class page 
driver.quit()

At the end of the script execution, you can see that although we have set the hidden time to 60 seconds, the page has been loaded in about 5 seconds (see my results below) and you can click the "open class" button. Here is the result of my 1 execution, showing the entire load time.


>>>access time is : 5.717327117919922

Dominant waiting

Dominant waiting for use when you need to import the selenium. webdriver. support. wait. WebDriverWait, API is as follows:


WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
driver: needless to say, you define the WebDriver browser (Chrome, Firefox, etc.) timeout: maximum wait time in seconds poll_frequency: the default time of searching for elements is 0.5 seconds (0.5 seconds is not set), that is to say, the default time is 0.5 seconds to check whether the element of a secondary search exists. If it is found, the entire explicit wait will end; otherwise, it will continue to wait 0.5 seconds and search again ignored_exceptions=None: exception message sent over time, NoSuchElementException is sent by default

Since explicit waiting may require confirmation of the presence of an element, general 1 is also used in conjunction with the following two methods


until(method, message='')
until_not(method, message='')
method: method of until() means to call the method provided by the driver as an argument until it returns not False, method of until_not() just until it returns False message: exception message sent over time

Note that method() must be callable with the s 84en__ () method. In the example above, let's rewrite 1.


# coding = utf-8
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import time

driver = webdriver.Chrome()
class button():
 def __call__(self, driver):
  if driver.find_element_by_id('js_love_url'):
   return True
  else:
   return False

driver.implicitly_wait(60)
time_start = time.time()
driver.get('https://www.163.com/')

# driver.find_element_by_id('js_love_url').click()
WebDriverWait(driver,2,0.5).until(button()) 
time_end = time.time()
print('access time is : ', time_end - time_start)
time.sleep(2)
driver.quit()

After looking at this example, you might wonder why I didn't report an error when I explicitly set the explicit wait to 2 seconds. Since we also set the implicit wait time, the longest of the two is the actual wait time, so in this example, the wait time is still 60 seconds.

conclusion

1. Selenium can take three kinds of waiting, and the most intelligent one is the explicit waiting WebDriverWait().
2. When the implicit wait and the explicit wait exist at the same time, the longest waiting time of the two is taken as the effective waiting time
3. method() of until(method()) in explicit wait is a callable method, which can be defined by itself or anonymous functions, which we will discuss in more detail later
4. Implicit wait setting is set once, that is, throughout the entire script, while mandatory wait must be set at every place where wait is needed


Related articles: