[Python automated testing]: 3 ways to wait for elements

Why do we need to wait for elements?

1、 某些元素或者操作,只有在加载完成后才能够正确定位到。
2、 若是元素还没有加载出来,就执行查询该元素的代码,就会出现错误:no such element

1. Element waiting method 1: sleep() forces waiting

【Grammar Realization】

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

【advantage】

  • easy to understand

【shortcoming】

  • The sleep time is not easy to grasp:
    • If the dormancy time is set too short, and the dormancy end element has not been loaded, the program will report an error;
    • If the sleep time is set too long, the program is still waiting after the elements have been loaded, which will waste time and affect the overall operating efficiency of the code.

2. Element waiting method 2: implicitly waiting implicitly_wait()

  • driver.implicitly_wait(5)
  • The default is 0s wait, the value after setting is the maximum timeout time, and the waiting time is the time when the entire page is fully loaded

advantage

  • Acts globally: Add after initializing the driver, and all subsequent codes will be affected
  • The set time is 整个页面the maximum loading time, non-necessary waiting time (if the entire page has been loaded before the set maximum timeout time, the following script will be executed immediately)

shortcoming

  • If the entire page is still not loaded after the set maximum timeout expires, an error will be reported: no such element

3. Element waiting method 3: explicitly wait for WebDriverWait()

  • Wait 指定元素, the element object can be returned when the timeout is reached; if the timeout is not reached, an exception is thrown: TimeoutException.
  • In short, it is to operate until the element appears, and throw an exception if it times out.
  • The result of WebDriverWait() returns the positioned element
#导包
from selenium import webdriver
from selenium.webdriver.common.by import By
# 导入显式等待的包
from selenium.webdriver.support.ui import WebDriverWait

# 语法   
element = WebDriverWait(自定义浏览器驱动名称, 最大超时时间, 检测的时间间隔).until(定位元素的方法, 错误信息)
  • example
# 导包
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

from time import sleep

# 生成谷歌浏览器对象
driver = webdriver.Chrome()

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

# 在百度搜索输入框输入“python自动化”,并点击“百度一下”按钮
driver.find_element(By.ID, 'kw').send_keys("python自动化")
driver.find_element(By.CSS_SELECTOR, '#su').click()

# 显式等待元素
element = WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "办公")), '没有找到元素')
element.click()

# 返回的页面停留5秒钟
sleep(5)
# 关闭浏览器
driver.quit()

3.1 Import excepted_conditions module

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
  • Check if an element exists . For example: how to judge that the alert pop-up window has come out; how to judge the elements of winter, etc.

3.1.1 title_is()

  • Determine whether the title of the current page is true 完全等于(==)预期字符串, and the return result is Boolean
# 导包
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

# 生成浏览器驱动对象
driver = webdriver.Chrome()
# 打开百度首页
driver.get('https://www.baidu.com')

# 判断返回的页面标题是否和预期一致,判断结果为布尔类型
result = EC.title_is('百度一下,你就知道')(driver)
# 打印返回结果
print(result)

3.1.2 title_contains()

  • Determine whether the title of the current page 包含预期字符串is the Boolean type
# 导包
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

# 生成浏览器驱动对象
driver = webdriver.Chrome()
# 打开百度首页
driver.get('https://www.baidu.com')

# 判断返回的页面标题是否包含预期字符串,判断结果为布尔类型
result2 = EC.title_contains('知道')(driver)

# 打印返回结果
print(result2)

3.1.3 presence_of_element_located() Method

def presence_of_element_located(locator: Any) -> (driver: {
    
    find_element}) -> Any
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
locator - used to find the element returns the WebElement once it is located
# 导包
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# 语法 
element = WebDriverWait(自定义浏览器驱动名称, 最大超时时间, 检测的时间间隔).until(EC.presence_of_element_located((By.属性名, '属性值'), 错误信息)
  • A WebDriverWait method that waits for an element to appear on the page. It can tell whether the page is loaded by looking for elements on the page.
# 导包
# 导包
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

from time import sleep

# 生成谷歌浏览器对象
driver = webdriver.Chrome()

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

# 在百度搜索输入框输入“python自动化”,并点击“百度一下”按钮
driver.find_element(By.ID, 'kw').send_keys("python自动化")
driver.find_element(By.CSS_SELECTOR, '#su').click()

# 显式等待元素
element = WebDriverWait(driver, 5, 0.5).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "办公")), '没有找到元素')
element.click()

# 返回的页面停留5秒钟
sleep(5)
# 关闭浏览器
driver.quit()

Guess you like

Origin blog.csdn.net/Lucifer__hell/article/details/129516816