Three element waiting methods for automated testing (python3.10+selenium4)

1. Setting the meaning of waiting

Because some elements or certain operations can only be located after loading. It is wrong to execute the script when writing an automated script, suggesting that the element is not found. The reason is: because the element has not been loaded, the search code has already been executed, and the element cannot be found naturally. The solution is: you can wait until the element is loaded and then execute the statement to search for the element.

Two, three element waiting methods

1, sleep forced to wait

time.sleep(5), the unit is s, is to let the thread sleep directly, and there is no need to do anything in these few seconds.

Pros: Simple and straightforward.

Disadvantages: If the sleep waiting time is set too short, the element has not been loaded yet, and the program reports an error. If the sleep setting wait time is too long, the element has already been loaded, and the program is still waiting, which wastes time and affects the overall operating efficiency of the code.

from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep

# 定义一个谷歌浏览器的对象
driver = webdriver.Chrome()

# 打开百度页面
driver.get("https://www.baidu.com/")

# 定位到百度输入框,搜索热点新闻
driver.find_element(By.ID, "kw").send_keys("热点新闻")

# 定位到百度一下按钮,并且点击
driver.find_element(By.ID, "su").click()

# 加上强制等待,才能定位到元素
sleep(3)

# 从返回的结果页面,通过模糊匹配定位到包含了“腾讯网”的超连接
# partial_link_text定位:定位的链接文本内容在整个页面当中唯一的出现一次,那么可以准确定位到元素,否则默认返回第一个
# .click()点击的操作
driver.find_element(By.PARTIAL_LINK_TEXT, "腾讯网").click()

# 延时3秒
sleep(5)

# 退出浏览器
driver.quit()

2. implicitly_wait() implicitly waits

implicitly_wait(20) defaults to 0s wait, and the value after setting is the maximum timeout time.

Advantages: Adding implicitly_wait(10) in the front part of the code will be effective during the entire program running (applicable to the global, directly added after the initialization of the driver, and the subsequent code will be affected), and will wait for the element to be loaded.

Disadvantages: If the entire page is not loaded within the set time, a NosuchElementError error will be reported. If the element is loaded within 10s, the following script will be automatically executed instead of waiting for 10s. It is necessary to load the entire page to execute the code, which affects the execution efficiency of the code. In general, the result we want is to execute the code only after loading the element I want to locate, without waiting for the entire page to be fully loaded. Execute the code.

from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep

# 定义一个谷歌浏览器的对象
driver = webdriver.Chrome()

# 打开百度页面
driver.get("https://www.baidu.com/")

# 定位到百度输入框,搜索热点新闻
driver.find_element(By.ID, "kw").send_keys("热点新闻")

# 定位到百度一下按钮,并且点击
driver.find_element(By.ID, "su").click()

# 加上隐式等待
driver.implicitly_wait(5)

# 从返回的结果页面,通过模糊匹配定位到包含了“腾讯网”的超连接
# partial_link_text定位:定位的链接文本内容在整个页面当中唯一的出现一次,那么可以准确定位到元素,否则默认返回第一个
# .click()点击的操作
driver.find_element(By.PARTIAL_LINK_TEXT, "腾讯网").click()

# 延时3秒
sleep(5)

# 退出浏览器
driver.quit()

3. WebDriverWait() explicitly waits

Explicit waiting is to wait until an element appears or is clickable, etc., and the element object can be returned after waiting; if it has not waited after the timeout expires, then TimeoutException is thrown. (In short, do not operate until the element appears, and report an exception if it times out).

expected_conditions as EC:

Judging whether an element exists, how to judge that an alert pop-up window has come out, how to judge a series of judgments such as dynamic elements, a series of scene judgment methods are collected in the expected_conditions module of selenium.

Case 1: title_is: Determine whether the title of the current page is completely equal to (==) the expected string, and return a Boolean value.

# from selenium.webdriver.support.ui import WebDriverWait
# 判断一个元素是否存在,如何判断alert弹窗出来了,如何判断动态的元素等等一系列的判断,在selenium的expected_conditions模块收集了一系列的场景判断方法
from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.common.by import By
from selenium import webdriver

# 案例一
# title_is:判断当前页面的title是否完全等于(==)预期字符串,返回布尔值。
driver = webdriver.Chrome()

driver.get("https://www.baidu.com/")

title = EC.title_is("百度一下,你就知道")(driver)
print(title)

Case 2: title_contains : Determine whether the title of the current page contains the expected string, and return a Boolean value.

from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

# 案例二
# title_contains:判断当前页面的title是否包含预期字符串,返回布尔值
driver = webdriver.Chrome()

driver.get("https://www.baidu.com/")

title = EC.title_contains("百度")(driver)
print(title)

Case three:

presence_of_element_located : Judging whether an element has been added to the dom tree does not mean that the element must be visible.

WebDriverWait function:

driver: browser driver

timeout: the maximum timeout time, the default is in seconds

poll_frequency: detection interval (step size), the default is 0.5S

ignored_exceptions: Exception information after timeout, by default NoSuchElementException is thrown

The WebDriverWait class provides two methods for handling waiting conditions:

until(method, message=' '): Call this method to provide the driver as a parameter until the return value is True

until_not(method, message=' '): Call this method providing the driver as a parameter until the return value is False

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep

# 定义一个谷歌浏览器的对象
driver = webdriver.Chrome()

# 打开百度页面
driver.get("https://www.baidu.com/")

# 定位到百度输入框,搜索热点新闻
driver.find_element(By.ID, "kw").send_keys("热点新闻")

# 定位到百度一下按钮,并且点击
driver.find_element(By.ID, "su").click()

# 添加一个显式等待
element = WebDriverWait(driver, 10, 0.5, ignored_exceptions=None).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "腾讯网")), "找不到")

element.click()

sleep(2)

driver.quit()

Guess you like

Origin blog.csdn.net/Little_Carter/article/details/128915678