How to use explicit waiting and implicit waiting in Web automation testing?

Posted by ranman on Mon, 03 Jan 2022 20:27:34 +0100

This article is excerpted from the internal textbook of Hogwarts Testing Institute

In practice, the waiting mechanism can ensure the stability of the code and ensure that the code will not be constrained by network speed, computer performance and other conditions.

Waiting means that when running code, if the rendering speed of the page can not keep up with the running speed of the code, it is necessary to artificially limit the speed of code execution.

In Web automation, you generally have to wait until the page elements are loaded, otherwise you will report various errors such as missing elements, which requires waiting in some scenarios.

There are three common waiting methods:

  • Implicit waiting

  • Explicit wait

  • Forced waiting

The usage scenarios of these three modes will be introduced one by one later.

Implicit waiting

Set a waiting time, poll to find (default 0.5 seconds) whether the element appears, and throw an exception if it does not appear. This is also the most common waiting method.
Implicit waiting is global and applies to the whole session
In other words, as long as you set an implicit wait, you don't need to set it later. If you set the implicit wait again, the last one will overwrite the previous one.
An implicit wait is triggered when an element is found in the DOM structure and the element is in a state where it cannot interact immediately.


Explicit wait

Explicit waiting is to define a waiting condition in the code and execute subsequent code after triggering the condition. Use until() and until in the WebDriverWait class_ not()
Method, you can wait according to the judgment conditions.
The program judges the conditions every other period of time (0.5 seconds by default). If the conditions are true, execute the next step, otherwise continue to wait until the set maximum time is exceeded.

# Import display wait from selenium webdriver. support. wait import WebDriverWaitfrom selenium. webdriver. support import expected_ conditions  
# Set the maximum waiting time of 10 seconds. Click webdriverwait (driver, 10) for the element of wait (by. Tag_name, "title") until(    expected_conditions.element_to_be_clickable((By.TAG_NAME, "title")))

Here, you can import expected_ The conditions library is used to meet the scenarios required for explicit waiting, but sometimes the scenarios are expected_conditions
The library can not be well solved. At this time, you need to write your own methods to meet specific scenarios.

Hypothesis: to judge that a certain element exceeds the specified number, you can perform the following operations.


def ceshiren():    # Define a method    def wait_ele_for(driver):        # Assign the number of found elements to eles        eles = driver.find_elements(By.XPATH, '//*[@ id = "site text logo"] ') # return len (ELES) > 0 driver = webdriver Chrome()    driver. get(' '# display wait for 10 seconds, direct wait_ ele_ For returns true webdriverwait (driver, 10) until(wait_ele_for)

Forced waiting

Forced wait is to make the thread sleep for a certain period of time. Forced waiting is generally used when implicit waiting and explicit waiting do not work.

# Wait ten seconds time sleep(10)


Visit the tester community: , click classification, and then click Q & A area:

When you click classification, the element has not been loaded yet, so you need to wait implicitly. When you click the Q & A area, the element has been loaded, but it is still in a non clickable state. At this time, you need to explicitly wait.

#Import dependency import timefrom selenium import webdriverfrom selenium webdriver. common. by import Byfrom selenium. webdriver. support import expected_ conditionsfrom selenium. webdriver. support. wait import WebDriverWait  
class TestHogwarts():    def setup(self):        self.driver = webdriver.Chrome()        self.driver.get(' / '# join implicit wait for self driver. implicitly_ wait(5)  
    def teardown(self):        #Forced wait time sleep(10)        self. driver. quit()  
    def test_hogwarts(self):        #Element positioning, here category_name Is a tuple.        category_name = (By.LINK_TEXT, "Open source project")        # Join explicit wait        WebDriverWait(self.driver, 10).until(            expected_conditions.element_to_be_clickable(category_name))        # Click open source project self driver. find_ element(*category_name). click()

That's all for implicit waiting and display waiting. Please look forward to the next issue of "web control positioning and common operations"

** _
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Click for more information