自动化测试:Selenium中的时间等待

在 Selenium 中,时间等待指在测试用例中等待某个操作完成或某个事件发生的时间。Selenium 中提供了多种方式来进行时间等待,包括使用 ExpectedConditions 中的 presence_of_element_located 和 visibility_of_element_located 方法等待元素可见或不可见,以及使用 until 和 when 等待某个特定操作完成或某个特定事件发生。

下面是一些示例代码:

  1. 使用 presence_of_element_located 方法等待某个元素加载
python 复制代码
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')))
  1. 使用 until 等待某个特定操作完成
python 复制代码
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()
  1. 使用 when 等待某个特定事件发生
python 复制代码
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()

需要注意的是:

时间等待的时间长度应该根据具体的测试用例来确定,并且应该尽可能地模拟真实用 户的行为,以保证测试用例更加准确和可靠。

  1. 使用 ExpectedConditions.visibility_of_element_located 方法等待某个元素可见
python 复制代码
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')))

需要注意的是:

visibility_of_element_located 方法只能用于等待某个元素可见,而不能用于等待某个元素不可见

另外,visibility_of_element_located 方法可能会比其他方法消耗更多的时间,因为它会等待元素变为可见,而不是仅仅判断元素是否可见。因此,在使用该方法时需要根据具体情况进行调整

  1. 使用 ExpectedConditions.title_contains 方法等待页面标题包含指定内容
python 复制代码
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'))

需要注意的是:

title_contains 方法只能用于等待页面标题包含指定内容,而不能用于等待页面标题不包含指定内容

另外,title_contains 方法可能会比其他方法消耗更多的时间,因为它会等待页面标题完全包含指定内容,而不是仅仅判断页面标题是否包含指定内容。因此在使用该方法时需要根据具体情况进行调整。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
睡不醒的kun6 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌9 小时前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
三体世界13 小时前
测试用例全解析:从入门到精通(1)
linux·c语言·c++·python·功能测试·测试用例·测试覆盖率
程序员东岸14 小时前
C语言入门指南:字符函数和字符串函数
c语言·笔记·学习·程序人生·算法
武子康20 小时前
AI-调查研究-76-具身智能 当机器人走进生活:具身智能对就业与社会结构的深远影响
人工智能·程序人生·ai·职场和发展·机器人·生活·具身智能
Nan_Shu_6141 天前
Web前端面试题(1)
前端·面试·职场和发展
YuTaoShao1 天前
【LeetCode 每日一题】3000. 对角线最长的矩形的面积
算法·leetcode·职场和发展
Freed&1 天前
《没有架构图?用 netstat、ss、tcpdump 还原服务连接与数据流向》
网络·测试工具·tcpdump
007php0071 天前
Redis高级面试题解析:深入理解Redis的工作原理与优化策略
java·开发语言·redis·nginx·缓存·面试·职场和发展
程序员杰哥1 天前
什么是Jmeter? Jmeter工作原理是什么?
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·测试用例