Selenium Automation Tutorial 03: 3 ways to delay waiting

We often encounter that when using Selenium to operate an element on the page, we need to wait for the page to load before we can operate it. Otherwise, the element on the page does not exist and an exception will be thrown. Or when encountering AJAX asynchronous loading, we need to wait for the element to be loaded before we can operate. When performing UI automation testing, you need to wait for the element to be loaded before operating on the element, otherwise an error will be reported if the element cannot be found. There are three ways to delay waiting: forced waiting, implicit waiting and explicit waiting.

1. Forced waiting: Executed after executing the get method. Forced waiting is a delayed waiting implemented through the time.sleep() method. It will suspend the execution of the program for a specified time. Everyone should be familiar with it. Sleep means forced waiting. Hard waiting is also called forced waiting and thread sleep. Forced waiting, as the name suggests, forces you to wait. You have to wait and you have to wait. There is no negotiation. Regardless of whether the page is loaded, execution continues after a mandatory waiting time. Since this method is relatively rigid and inflexible, it will cause the script to run longer, so it is recommended to use it as little as possible.

import time  
time.sleep(5)  # 暂停程序执行5秒

2. Implicit wait: Implicit wait is a global wait that sets a maximum waiting time for all elements on the page. If the page element is loaded within the specified time, subsequent operations will be performed; if the time limit is exceeded, a timeout exception will be thrown. The implicit wait time can be set through the webdriver.implicitly_wait() method.

from selenium import webdriver      #导包
driver = webdriver.Chrome()         #获取浏览器驱动对象
driver.implicitly_wait(20)          #隐示等待,传入等待时间

3. Explicit waiting: Explicit waiting is a more flexible way of waiting. It allows you to specify a condition and perform subsequent operations only when the condition is met. Explicit waits can be set through the WebDriverWait class and the expected_conditions module.

# @Author : 小红牛
# 微信公众号:WdPython
import time
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

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
# 设置等待时间10s
wait = WebDriverWait(driver, 10)
# 设置判断条件:等待id='kw'的元素加载完成
input_element = wait.until(EC.presence_of_element_located((By.ID, 'kw')))

input_element.send_keys('李白')
time.sleep(2)
driver.quit()

3.1. Instructions for using WebDriverWait parameters:

1.WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
driver: 浏览器驱动
timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)
poll_frequency: 每次检测的间隔时间,默认是0.5
ignored_exceptions:超时后的异常信息,默认情况下抛出NoSuchElementException异常

2.until(method,message='')
method: 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不是False
message: 如果超时,抛出TimeoutException,将message传入异常

3.until_not(method,message='')
until_not 与until相反,until是当某元素出现或什么条件成立则继续执行,
until_not是当某元素消失或什么条件不成立则继续执行,参数也相同。

3.2.What are the conditions for expected_conditions?

1. Determine whether the title is consistent with expectations

title_is

2. Determine whether the title contains the expected string

title_contains

3. Determine whether the specified element is loaded

presence_of_element_located

4. Determine whether all elements have been loaded

presence_of_all_elements_located

5. Determine whether an element is visible. Visible means that the element is not hidden, and the width and height of the element are not equal to 0. The incoming parameter is a locator of tuple type.

visibility_of_element_located

6. Determine whether the element is visible. The incoming parameter is the positioned element WebElement.

visibility_of

7. Determine whether an element is invisible or does not exist in the DOM tree

invisibility_of_element_located

8. Determine whether the text of the element contains the expected string

text_to_be_present_in_element

9. Determine whether the value of the element contains the expected string

text_to_be_present_in_element_value

10. To determine whether the frame can be entered, you can pass in the locator tuple or directly pass in the positioning method: id, name, index or WebElement

frame_to_be_available_and_switch_to_it

11. Determine whether an alert appears

alert_is_present

12. Determine whether the element is clickable

element_to_be_clickable

13. Determine whether the element is selected, generally used in drop-down lists, passing in the WebElement object

element_to_be_selected

14. Determine whether the element is selected

element_located_to_be_selected

15. Determine whether the selected state of the element is consistent with expectations. Pass in the parameters: the positioned element. If equal, return True, otherwise return False

element_selection_state_to_be

16. Determine whether the selected state of the element is consistent with expectations. Pass in the parameter: the positioning of the element. If equal, return True, otherwise return False.

element_located_selection_state_to_be

17. To determine whether an element is still in the DOM, pass in the WebElement object to determine whether the page has been refreshed.

staleness_of

complete! ! thanks for watching

----------★★Historical blog post collection★★----------
My zero-based Python tutorial, Python introduction to advanced video tutorial Py installation py project Python module Python crawler Json
Insert image description here

おすすめ

転載: blog.csdn.net/gxz888/article/details/135288032
03