English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to set waiting time in Python selenium

The Selenium WebDriver testing method we introduced earlier is based on web pages. The previous examples all used simple web pages for operations, which may not feel the loading process of the web page, but in the actual application process, the loading of web pages does consume a certain amount of time. Your script has started running, but the element you want to locate has not been loaded yet, at this point, an error will be reported that the element cannot be found. It is obvious that a script that does not consider the loading time is not a successful script. Today, we will introduce how to set the waiting time.

Three methods of waiting

time.sleep(n)

Force wait for n seconds. A function of Python itself, included in the time package, and needs to import the time package before use. We often use this waiting method in our previous examples to make it easier for everyone to see the results of the script execution. This waiting method is very clumsy, no matter how the page loads, it must wait for n seconds, which can be said to be very unsmart.

implicitly_wait(n)

Wait up to n seconds, and end the waiting early if the page is fully loaded within n seconds. This is a waiting method provided by WebDriver and is also known as implicit wait. It is a bit smarter than forced wait, but if the page itself contains a very large video file or something like that, even if the element we need to locate has already been loaded at the beginning, we still have to wait for all the files to be loaded before the script can continue to execute, which is still somewhat of a drawback.

WebDriverWait(n)

Wait up to n seconds, check the existence of the required element at regular intervals within n seconds, and end the waiting early if the element exists. This is also a waiting method provided by WebDriver and is also known as explicit wait. This waiting is a bit smarter than implicit wait, ignoring the entire page loading, and ending the waiting as soon as the required element exists.

Example

Forced wait has been used in previous examples, so let's take a look at the two waiting methods provided by WebDriver

Implicit wait

In fact, implicit wait has also been used in the previous introduction, but it was not mentioned specifically how to use it. This time we will open the homepage of NetEase, which is a portal website with a lot of content. According to different internet speeds, it is estimated to take about10seconds to load completely, and we set the waiting time to60 seconds, and then calculate how long it takes from opening the page to clicking the [Public Class] button on the page navigation bar.

# coding = utf-8
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.implicitly_wait(60) #Implicit wait time60 seconds
time_start = time.time() #Record the time when the page is opened
driver.get('https://www.163.com/)
driver.find_element_by_id('js_love_url').click()
time_end = time.time() #Record the time after clicking the button
print('access time is: ', time_end - time_start) #Print the time difference, which is the actual consumption time
time.sleep(2) #强制等待2seconds, in order to observe, we indeed opened the [Public Class] page
driver.quit()

As we can see from the script execution end, although we set the implicit time to60 seconds, but5About a second (see the execution result below) the page has been fully loaded, and you can click the [Public Class] button. Below is one of my execution results, showing the entire loading time.

>>>access time is : 5.717327117919922

Explicit Wait

When using explicit wait, it is necessary to import the selenium.webdriver.support.wait.WebDriverWait class, the API is as follows:

WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
  • driver: Need to say, the WebDriver browser you define (Chrome, Firefox, etc.)
  • timeout: The longest waiting time, in seconds
  • poll_frequency: The time interval for searching for elements, the default is 0.5seconds (not specified is 0.5seconds), that is, the default is 0.5seconds to check whether the element to be searched for exists, if found, end the entire explicit wait, otherwise continue waiting 0.5seconds to search again
  • ignored_exceptions=None: The exception information sent when the timeout occurs, the default is NoSuchElementException

Since explicit wait may need to confirm the existence of an element, it is generally necessary to use the following two methods in conjunction

until(method, message='')
until_not(method, message='')
  • method: The method of until() means to call the method provided by the driver until it returns not False, and the method of until_not() is until it returns False
  • message: The exception information passed when the timeout occurs

Note that the method() must be a callable method, it must have the __call__() method. Let's rewrite the example above.

# 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 reading this example, you may have doubts, I explicitly wait and set it to2seconds, why is there no error? Because we also set the implicit wait time, and the longest time of the two is taken as the actual waiting time. Therefore, in this example, the waiting time is still60 seconds.

Summary

1Selenium can take three types of waiting, and the most intelligent one is WebDriverWait().
2When both implicit wait and explicit wait exist at the same time, the longest waiting time of the two is the effective waiting time.
3In explicit wait, the method() of until(method()) is a callable method, which can be defined by yourself, or can use anonymous functions and other methods, which we will discuss in detail later.
4Set implicit wait once, which runs through the entire script, and explicit wait must be set at each place that needs to wait.

That's all for this article. I hope it will be helpful to everyone's study, and I also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the network, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liabilities. If you find content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like