Common waiting methods for UI automation, these three are the most important

Common waiting methods for UI automation

In UI automation, the script is often unstable. Sometimes elements can be located, but sometimes they cannot be located. The above situation can be solved by waiting. Let's introduce the common waiting methods below.

1. Forced waiting: (sleep)

Forced waiting: Regardless of whether the browser element is loaded or not, the program has to wait for 3 seconds, and when the 3 seconds are up, continue to execute the following code. code show as below:

from selenium import webdriver # Import the webdriver in selenium 
from time import sleep # Import the sleep driver in the time module 

= webdriver.Chrome() 
driver.get("https://www.baidu.com") # Open the Baidu homepage website 
sleep (2) # Wait for two seconds 
driver.find_element_by_id("kw").send_keys("It stone software test") # Input it stone software test in the Baidu homepage input box 
driver.find_element_by_id("su").click() # Click the Baidu button 
sleep(2) 
driver.quit() # Close the browser

2. Implicitly wait: (implicitly_wait)

implicitly_wait(x): Set the waiting time to x seconds and wait for the element to be loaded. If the element is not loaded by the time, a NoSuchElementException error will be thrown. code show as below:

from selenium import webdriver # Import webdriver in selenium 
from time import sleep # Import sleep 

driver in time module = webdriver.Chrome() 
driver.implicitly_wait(30) # Implicit wait, up to 30 seconds 
driver.get("https ://www.baidu.com") # Open the Baidu homepage website 
driver.find_element_by_id("kw").send_keys("It Stone Software Test") #Input it Stone Software Test 
driver.find_element_by_id(" su").click() # Click the Baidu button 
driver.quit() # Close the browser

Note: Implicit waiting works on the entire driver cycle, so it only needs to be set once.

3. Display wait: (WebDriverWait)

Display waiting: Set a waiting time and a condition. Within the specified time, check whether the condition is true at regular intervals. If it is true, the program will continue to execute, otherwise it will prompt a timeout exception.

Typically the WebDriverWait class will be used in conjunction with the ExpectedCondition class. code show as below:

Specific parameters and methods of WebDriverWait:

WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None) 
driver: browser driver 
timeout: timeout, the longest waiting time 
poll_frequency: the interval between each detection, the default is 0.5 seconds 
ignored_exceptions: exception information after timeout, By default, a NoSuchElementException is thrown 

until(method, message='') 
method: During the waiting period, the incoming method is called at regular intervals until the return value is not False 
message: If it times out, a TimeoutException is thrown and the message is passed into exception

The commonly used judgment conditions in ExpectedCondition are as follows:

# Judging whether the specified element is loaded 
presence_of_element_located 
# Judging whether an element is 
visible visibility_of_element_located 
# Judging whether the frame can be cut in, you can pass in the locator tuple or directly the positioning method: id, name, index or WebElement 
frame_to_be_available_and_switch_to_it 
# Judging whether the element is selected 
element_located_to_be_selected 
# Determine whether the element can be clicked 
element_to_be_clickable:

The actual code is as follows:

from selenium import webdriver 
from selenium.webdriver.support.wait import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By # Introduce By positioning method 

driver = webdriver.Chrome() 
driver.get( 'https://www.baidu.com') #Open Baidu Homepage 
# Set the judgment condition: wait for the element with id='kw' to be loaded, set the browser: driver, wait time: 20s, check every 0.5 seconds Whether the element is loaded 
input_element = WebDriverWait(driver, timeout=20,poll_frequency=0.5).until(EC.presence_of_element_located((By.ID, 'kw'))) input_element.send_keys 
('Input data') # in the input box Enter the data that needs to be input 
driver.quit()

Finally: You can get a 216-page software test engineer interview collection document for free on my VX public account: [Automated Test Veteran Driver] . And the corresponding video learning tutorials are free to share! , which includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, packet capture tool topics, interface testing tools, advanced testing-Python programming, Web automated testing, APP automated testing, interface automated testing, testing Advanced continuous integration, test framework development test framework, performance testing, security testing, etc. You can also interact with me

Friends who like software testing, if my blog is helpful to you, if you like my blog content, please "like", "comment" and "favorite" with one click!

The following are supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/myh919/article/details/132104310