An article teaches you to quickly understand the three waiting methods in web automated testing

Preface

Many web pages today are loaded dynamically. If the content of the page changes, it takes time to render. When we do web automation testing, because the code is automatically executed, when the code is executed, it is possible that the element loaded in the previous step has not been loaded, and a NoSuchElementException error will be reported. If this error is reported, it is very likely to be located. The expression is incorrect, or the page elements have not been loaded yet. To solve this problem, we can improve the stability of the script by setting up some waiting mechanisms. When doing web automation, the following three waiting methods are used:

  • 1. Forced waiting:time.sleep()

  • 2. Implicit waiting: driver.implicitly_wait(10)

  • 3. Explicit waiting: (waiting for the element to be in a certain state: existing, visible, clickable, etc.)

 

WebDriverWait(driver, 30, 0.2).until(
    EC.visibility_of_element_located(
        (By.XPATH, '//input[@id="u"]')
    )
)

Next, we will introduce the use of the three waiting methods in detail.

1. Forced waiting

强制等待,也就是python中的

time.sleep(), when the program is executed totime.sleep(), the program will enter the sleep state (which is what we call waiting), and will continue to execute after the sleep time has passed.

使用强制等待的缺点,程序在执行的过程中,不管要操作的元素是否出现,只要执行到

 

time.sleepIt will wait, which increases the execution time of the program to a certain extent.

Case:

import time
from selenium import webdriver
driver = webdriver.Chrome()
# 设置隐式等待最大时间为10秒
driver.implicitly_wait(10)
 
driver.get("http://www.baidu.com")
 
 

2. Implicit waiting

Implicit waiting: Set a global waiting time for the driver object, which is valid throughout the entire life cycle of the driver object. Whether you are searching for an element through the driver or performing some other operations on the element, if the element does not exist, implicit waiting will cause the driver to wait for a certain period of time before searching for an element. Check to see if it appears every once in a while. If not, continue waiting. When the element appears, stop waiting and continue execution. If the waiting time exceeds the set maximum time, a waiting timeout error will be thrown!

Case

import time
from selenium import webdriver
driver = webdriver.Chrome()
# 设置隐式等待最大时间为10秒
driver.implicitly_wait(10)
 
driver.get("http://www.baidu.com")
 
 

 

3. Explicit wait

Explicit wait: usually used to wait for an element to be in a specific state. In many cases, implicit waiting cannot meet our needs. For example, the page element already exists, but is in an invisible state. At this time, related operations cannot be performed on the element. At this time, we need to wait for the element to be visible through explicit waiting. status.

(1) Basic use of explicit waiting

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
# =============步骤拆解================
# 第一步:创建一个等待对象
wait = WebDriverWait(driver, 30, 0.2)
# 第二步:定义元素查找对象
# located = ("定位方式",'定位表达式')
# 比如通过xpath
located = (By.XPATH, '//input[@id="u"]')
# 第三步: 定位的等待条件
conditions = EC.visibility_of_element_located(located)
# 第四步:通过等待计时器对象去找
wait.until(conditions)
 
# ============一行代码==================
WebDriverWait(driver, 30, 0.2).until(
    EC.visibility_of_element_located(
        (By.XPATH, '//input[@id="u"]')
    )
)
  
 
 

(2), display the commonly used conditions for waiting

Summarize:

  • Forced waiting: Foolish waiting method, not smart
  • Implicit wait: universal, only needs to be set once, very smart (wait and go, no time wasted), the shortcomings can only be used to find elements and wait to see if the elements can be found.
  • Explicit wait: Used when waiting for an element to meet specific conditions. It is very smart (wait until it is there and leave without wasting time)
  • Question: When to use implicit waiting and when to use explicit waiting in work?
  • Implicit is universal and should be set after creating the driver. If an error occurs in the implicit wait code, the corresponding explicit wait will be added based on the error conditions.
  • Question: Why wait?
  • Improve the stability of the program (no page loading elements will be slow, elements cannot be found, and code execution errors will not be reported due to network and other reasons)

Thank you to everyone who reads my article carefully. There is always a courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly:

 

 

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends, and this warehouse also accompanies Thousands of test engineers have gone through the most difficult journey, and I hope it can help you!Friends in need can click on the small card below to receive it 

Guess you like

Origin blog.csdn.net/okcross0/article/details/134928141