【selenium】设置元素等待

简介: 简介:【selenium】设置元素等待

一、前言


selenium中有三种时间等待:

  • 强制等待:sleep
  • 隐式等待:implicitly_wait
  • 显示等待:WebDriverWait

其中,这三种方法各有优缺点:image.png


二、强制等待


让程序暂停运行一定的时间,等待时间达到要求的时间后继续运行。

使用前,需要先导入time模块:

import time

在前面的文章中,已经多次使用过,这里就不做演示了。

需要注意的是,对于定位不到元素的时候,从耗时方面来讲,隐式等待强制等待没什么区别。


三、隐式等待


implicitly_wait()默认参数的单位为秒,默认值为0。

如果在最大超时时间内找到元素了,会开始执行下一操作,未找到元素,则会抛出NoSuchElementException 异常。

以百度为例:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
# 访问百度
driver.get('https://baidu.com')
start = time.time()
# 设置最大等待时间为3秒
driver.implicitly_wait(3)
try:
    # 使用id定位一个不存在的元素
    driver.find_element(By.ID, 'yyds')
except Exception as exception:
    # 打印异常
    print(exception)
    # 打印程序运行时间
    print(f'耗时:{time.time()-start}')
# 关闭所有页面
driver.quit()

099eb95220b1434aa665cda2a8ea1103.gif


四、显示等待


需要导入模块:

from selenium.webdriver.support.ui import WebDriverWait

WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)


driver: 传入WebDriver实例;

timeout:指最大超时时间,默认单位为秒;

poll_frequency:调用until或until_not方法,每隔一定时间不断尝试是否能找到页面元素,默认间隔是0.5s,可自行调整间隔时间。

ignored_exceptions:超时后的异常信息,默认情况下NoSuchElementException 异常。

WebDriverWait一般和until()和until_not()配合使用:


until() 当某元素出现 或 某条件成立则继续执行

until_not 当某元素消失 或 某条件不成立则继续执

until或until_not中的method参数一定要是可以调用的对象,我们可以用selenium提供的expected_conditions,提供一些场景的判断。

但使用expected_conditions,需先导入:

from selenium.webdriver.support import expected_conditions as EC

常用的expected_conditions方法如下:image.png

以百度为例:

  • 定位一个存在的元素:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
# 访问百度
driver.get('https://www.baidu.com')
# 设置显式等待,超时时长最大为5s,每隔1s查找元素一次
# 根据id定位到元素位置
element = WebDriverWait(driver, 5, 1).until(
    EC.presence_of_element_located(('id', 'kw')))
# 在定位到的地方,输入CSDN
element.send_keys('CSDN')

d0e0cce747884a0f97ad6ea38c58ee46.gif

  • 定位一个不存在的元素
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
driver = webdriver.Chrome()
# 访问百度
driver.get('https://www.baidu.com')
# 设置显式等待,超时时长最大为5s,每隔1s查找元素一次
# 根据id定位到元素位置
element = WebDriverWait(driver, 5, 1).until(
    EC.presence_of_element_located((By.ID, 'csdn')), message='超时了哟。。。')

得到提示:

selenium.common.exceptions.TimeoutException: Message: 超时了哟。。。

五、参考文章


Selenium自动化测试-设置元素等待

相关文章
|
1月前
|
Web App开发 测试技术
使用selenium轻松实现元素拖拽
本文介绍了如何使用Selenium进行Web自动化测试中的元素拖拽操作。通过`ActionChains`类,我们可以模拟用户拖拽行为,确保测试覆盖到页面布局调整等交互功能。示例代码展示了如何定位元素并执行拖拽,以及在实际场景中改变页面布局的应用。利用Selenium的拖拽功能,可提升自动化测试的真实性和效率。
17 0
|
1月前
|
JavaScript
selenium元素等待及滚动条滚动
selenium元素等待及滚动条滚动
20 2
|
3月前
|
Python
python+selenium 判断元素是否存在
python+selenium 判断元素是否存在
37 0
|
3月前
|
API Python
python+selenium设置下载路径
python+selenium设置下载路径
31 0
|
3月前
|
API Python
python+selenium设置下载路径21 / 100
python+selenium设置下载路径21 / 100
28 0
|
4月前
设置selenium默认下载位置
设置selenium默认下载位置
99 1
|
8月前
|
JavaScript
selenium--高亮显示正在操作的元素
selenium--高亮显示正在操作的元素
|
8月前
selenium--页面元素是否可见和可操作
selenium--页面元素是否可见和可操作
|
8月前
selenium--拖拽页面元素
selenium--拖拽页面元素
selenium--拖拽页面元素
|
8月前
|
前端开发
selenium--页面元素相关的操作
selenium--页面元素相关的操作

热门文章

最新文章