Automated Testing: Time Waiting in Selenium

In Selenium, time wait refers to the time to wait for an operation to complete or an event to occur in a test case. Selenium provides a variety of ways to wait for time, including using the presence_of_element_located and visibility_of_element_located methods in ExpectedConditions to wait for the element to be visible or invisible, and using until and when to wait for a specific operation to complete or a specific event to occur.

Here is some sample code: 

1. Use the presence_of_element_located method to wait for an element to be loaded

from selenium import webdriver
 
from selenium.webdriver.common.by import By
 
from selenium.webdriver.support.ui import WebDriverWait
 
from selenium.webdriver.support import expected_conditions as EC
 
# 设置浏览器驱动
 
browser = webdriver.Chrome()
 
# 打开网站
 
browser.get('
https://example.com
browser.get('
 
# 等待某个元素加载
 
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'myelement')))

2. Use until to wait for a specific operation to complete

from selenium import webdriver
 
from selenium.webdriver.common.by import By
 
from selenium.webdriver.support.ui import WebDriverWait
 
from selenium.webdriver.support import expected_conditions as EC
 
# 设置浏览器驱动
 
browser = webdriver.Chrome()
 
# 打开网站
 
browser.get('
https://example.com
browser.get('
 
# 等待页面中的表单加载完成
 
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'myform')))
 
# 查找元素并提交表单
 
element.find_element_by_id('my-input').send_keys('my-value')
 
element.find_element_by_id('my-submit').click()

3. Use when to wait for a specific event to occur

from selenium import webdriver
 
from selenium.webdriver.common.by import By
 
from selenium.webdriver.support.ui import WebDriverWait
 
from selenium.webdriver.support import expected_conditions as EC
 
# 设置浏览器驱动
 
browser = webdriver.Chrome()
 
# 打开网站
 
browser.get('
https://example.com
browser.get('
 
# 等待页面中的表单提交完成
 
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'myform')))
 
# 等待表单提交完成事件触发
 
element.find_element_by_id('my-submit').click()

have to be aware of is:

The length of time to wait should be determined according to the specific test case, and the behavior of real users should be simulated as much as possible to ensure that the test case is more accurate and reliable.

4. Use the ExpectedConditions.visibility_of_element_located method to wait for an element to be visible

from selenium import webdriver
 
from selenium.webdriver.common.by import By
 
from selenium.webdriver.support.ui import WebDriverWait
 
from selenium.webdriver.support import expected_conditions as EC
 
# 设置浏览器驱动
 
browser = webdriver.Chrome()
 
# 打开网站
 
browser.get('
https://example.com
browser.get('
 
# 等待某个元素可见
 
element = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, 'my-element')))

 

have to be aware of is:

The visibility_of_element_located method can only be used to wait for an element to be visible, but not to wait for an element to be invisible.

Additionally, the visibility_of_element_located method may take more time than other methods because it waits for the element to become visible rather than just determining whether the element is visible. Therefore, the method needs to be adjusted according to the specific situation when using it.

5. Use the ExpectedConditions.title_contains method to wait for the page title to contain the specified content.

from selenium import ebdriver
 
from selenium.webdriver.common.by import By
 
from selenium.webdriver.support.ui import WebDriverWait
 
from selenium.webdriver.support import expected_conditions as EC
 
# 设置浏览器驱动
 
browser = webdriver.Chrome()
 
# 打开网站
 
browser.get('
https://example.com
browser.get('
 
# 等待页面标题包含指定内容
 
element = WebDriverWait(browser, 10).until(EC.title_contains('My Web Page'))

 

have to be aware of is:

The title_contains method can only be used to wait for the page title to contain the specified content, but cannot be used to wait for the page title to not contain the specified content.

In addition, the title_contains method may consume more time than other methods because it waits for the page title to completely contain the specified content, rather than just determining whether the page title contains the specified content. Therefore, the method needs to be adjusted according to the specific situation.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

How to obtain documents:

This document should be the most comprehensive and complete preparation warehouse for friends who want to engage in [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

All of the above can be shared. You only need to search the vx official account: Programmer Hugo to get it for free.

Guess you like

Origin blog.csdn.net/2301_79535544/article/details/133563322
Recommended