当前位置: 首页 > news >正文

公司做网站需要提供什么磁力兔子搜索引擎

公司做网站需要提供什么,磁力兔子搜索引擎,聊城网站建设哪家便宜,四川省住房和城乡建设厅网站无法进入目录 为什么需要等待Selenium 等待 API 简介隐式等待显式等待Fluent Wait等待策略的选择示例代码总结 正文 1. 为什么需要等待 在 Web 自动化测试中,等待是一个关键因素。网络应用通常是动态的,页面加载时间、元素的显示时间都可能不同步。直接操作这…

目录

  1. 为什么需要等待
  2. Selenium 等待 API 简介
  3. 隐式等待
  4. 显式等待
  5. Fluent Wait
  6. 等待策略的选择
  7. 示例代码
  8. 总结

正文

1. 为什么需要等待

在 Web 自动化测试中,等待是一个关键因素。网络应用通常是动态的,页面加载时间、元素的显示时间都可能不同步。直接操作这些元素可能会导致 NoSuchElementException 或者 ElementNotVisibleException 等错误。因此,等待机制可以帮助我们确保元素加载完成后再进行操作,从而提高测试的稳定性和可靠性。

2. Selenium 等待 API 简介

Selenium 提供了三种主要的等待机制:

  • 隐式等待 (Implicit Wait)
  • 显式等待 (Explicit Wait)
  • Fluent Wait

3. 隐式等待

隐式等待是全局设置的一种等待方式,它会在查找元素时等待一定的时间,默认时间为 0 秒。

from selenium import webdriverdriver = webdriver.Chrome()
driver.implicitly_wait(10)  # 设置隐式等待时间为 10 秒
driver.get("http://www.example.com")element = driver.find_element_by_id("element_id")

当元素未立即可见时,WebDriver 将会每隔一段时间检查一次,直到达到指定的等待时间。如果在规定时间内找到了元素,将立即返回,否则抛出 NoSuchElementException

4. 显式等待

显式等待是针对特定元素的等待,它在等待条件满足前会定期检查元素的状态。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()
driver.get("http://www.example.com")try:element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "element_id")))
finally:driver.quit()

WebDriverWait 结合 expected_conditions 模块可以灵活地等待元素的不同状态,如元素的可见性、元素的可点击性等。
expected_conditions 是 Selenium 提供的一组条件类,用于显式等待。这些条件可以用来判断特定元素或页面状态,以决定是否继续执行后续的操作。以下是一些常用的 expected_conditions 及其示例说明:

常用的 expected_conditions
  1. title_is
  2. title_contains
  3. presence_of_element_located
  4. visibility_of_element_located
  5. visibility_of
  6. presence_of_all_elements_located
  7. text_to_be_present_in_element
  8. text_to_be_present_in_element_value
  9. frame_to_be_available_and_switch_to_it
  10. invisibility_of_element_located
  11. element_to_be_clickable
  12. staleness_of
  13. element_to_be_selected
  14. element_located_to_be_selected
  15. alert_is_present
示例说明
1. title_is

等待页面标题等于指定值。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECWebDriverWait(driver, 10).until(EC.title_is("Expected Title"))
2. title_contains

等待页面标题包含指定文本。

WebDriverWait(driver, 10).until(EC.title_contains("Partial Title"))
3. presence_of_element_located

等待元素出现在页面上。

from selenium.webdriver.common.by import Byelement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "element_id"))
)
4. visibility_of_element_located

等待元素可见。

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "element_id"))
)
5. visibility_of

等待一个已知元素对象可见。

element = driver.find_element_by_id("element_id")
WebDriverWait(driver, 10).until(EC.visibility_of(element))
6. presence_of_all_elements_located

等待一组元素全部出现在页面上。

elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "class_name"))
)
7. text_to_be_present_in_element

等待元素中包含指定文本。

WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.ID, "element_id"), "Expected Text")
)
8. text_to_be_present_in_element_value

等待元素的值包含指定文本。

WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value((By.ID, "input_id"), "Expected Value")
)
9. frame_to_be_available_and_switch_to_it

等待 iframe 可用并切换到该 frame。

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "frame_name"))
)
10. invisibility_of_element_located

等待元素不可见。

WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.ID, "element_id"))
)
11. element_to_be_clickable

等待元素可点击。

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "clickable_element_id"))
)
element.click()
12. staleness_of

等待元素不再附加在 DOM 树上。

element = driver.find_element_by_id("stale_element_id")
WebDriverWait(driver, 10).until(EC.staleness_of(element))
13. element_to_be_selected

等待元素被选中。

element = driver.find_element_by_id("select_element_id")
WebDriverWait(driver, 10).until(EC.element_to_be_selected(element))
14. element_located_to_be_selected

等待特定定位器的元素被选中。

WebDriverWait(driver, 10).until(EC.element_located_to_be_selected((By.ID, "select_element_id"))
)
15. alert_is_present

等待警告框出现。

WebDriverWait(driver, 10).until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.accept()

通过这些 expected_conditions,你可以更加灵活地控制 Selenium 测试的等待逻辑,确保测试脚本在正确的时间点进行操作。

5. Fluent Wait

Fluent Wait 是显式等待的一种扩展,它允许我们定义等待的最大时间、轮询的频率以及在等待期间遇到的异常处理。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutExceptiondriver = webdriver.Chrome()
driver.get("http://www.example.com")wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[TimeoutException])
element = wait.until(EC.presence_of_element_located((By.ID, "element_id")))

Fluent Wait 通过指定轮询频率,可以更精确地控制等待行为。

6. 等待策略的选择

选择合适的等待策略取决于测试的具体需求:

  • 隐式等待 适用于大部分情况下的全局设置,但可能导致调试困难,因为它在所有元素查找时都生效。
  • 显式等待 提供了更精确的控制,适用于需要等待特定条件的场景。
  • Fluent Wait 是显式等待的高级版本,适用于需要自定义轮询频率和异常处理的复杂场景。

7. 示例代码

综合使用不同等待机制的示例代码:

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 ECdriver = webdriver.Chrome()
driver.get("http://www.example.com")# 设置隐式等待
driver.implicitly_wait(10)try:# 使用显式等待element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "element_id")))# 使用 Fluent Waitwait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[TimeoutException])element = wait.until(EC.element_to_be_clickable((By.ID, "clickable_element_id")))element.click()
finally:driver.quit()

8. 总结

等待机制在 Selenium 测试中起到了至关重要的作用。通过合理选择和使用隐式等待、显式等待和 Fluent Wait,可以大大提高自动化测试的稳定性和可靠性。希望这篇博客能帮助你更好地理解和应用 Selenium 的等待 API,在实际项目中写出更加健壮的测试用例。


希望这个博客大纲和详细内容对你有所帮助!如果有任何进一步的问题或需要更多示例,请随时告诉我。


文章转载自:
http://blackball.tbjb.cn
http://prebend.tbjb.cn
http://wooly.tbjb.cn
http://murra.tbjb.cn
http://distome.tbjb.cn
http://cagayan.tbjb.cn
http://seepage.tbjb.cn
http://genealogical.tbjb.cn
http://crossline.tbjb.cn
http://feathering.tbjb.cn
http://assyriology.tbjb.cn
http://maraschino.tbjb.cn
http://resonant.tbjb.cn
http://putamina.tbjb.cn
http://hexateuch.tbjb.cn
http://fishily.tbjb.cn
http://ezechiel.tbjb.cn
http://skiwear.tbjb.cn
http://auspice.tbjb.cn
http://run.tbjb.cn
http://menostaxis.tbjb.cn
http://thermotensile.tbjb.cn
http://oxygenation.tbjb.cn
http://talmessite.tbjb.cn
http://staminodium.tbjb.cn
http://fst.tbjb.cn
http://pronaos.tbjb.cn
http://epithelioid.tbjb.cn
http://chartography.tbjb.cn
http://harness.tbjb.cn
http://krakatau.tbjb.cn
http://hematocrit.tbjb.cn
http://antiketogenesis.tbjb.cn
http://dogsleep.tbjb.cn
http://rinderpest.tbjb.cn
http://bead.tbjb.cn
http://asmara.tbjb.cn
http://physiotherapeutic.tbjb.cn
http://rhodopsin.tbjb.cn
http://nowise.tbjb.cn
http://syncope.tbjb.cn
http://ocherous.tbjb.cn
http://businessman.tbjb.cn
http://tosspot.tbjb.cn
http://interisland.tbjb.cn
http://covered.tbjb.cn
http://testitis.tbjb.cn
http://sociality.tbjb.cn
http://cannister.tbjb.cn
http://stanchion.tbjb.cn
http://newsletter.tbjb.cn
http://burnable.tbjb.cn
http://lipoidal.tbjb.cn
http://unexcelled.tbjb.cn
http://diatonic.tbjb.cn
http://pronuclear.tbjb.cn
http://stockfish.tbjb.cn
http://grammaticaster.tbjb.cn
http://lioness.tbjb.cn
http://photosystem.tbjb.cn
http://ooze.tbjb.cn
http://murderess.tbjb.cn
http://skill.tbjb.cn
http://anticonvulsive.tbjb.cn
http://considerate.tbjb.cn
http://heniquen.tbjb.cn
http://skidder.tbjb.cn
http://escritoire.tbjb.cn
http://quartering.tbjb.cn
http://epaulette.tbjb.cn
http://hepatic.tbjb.cn
http://climax.tbjb.cn
http://personator.tbjb.cn
http://salivous.tbjb.cn
http://cete.tbjb.cn
http://transgressor.tbjb.cn
http://incalculably.tbjb.cn
http://lactary.tbjb.cn
http://divestment.tbjb.cn
http://aeacus.tbjb.cn
http://greenfly.tbjb.cn
http://lessness.tbjb.cn
http://genevieve.tbjb.cn
http://polynesia.tbjb.cn
http://craniad.tbjb.cn
http://uninviting.tbjb.cn
http://ethiopian.tbjb.cn
http://exact.tbjb.cn
http://pornography.tbjb.cn
http://reminiscential.tbjb.cn
http://primacy.tbjb.cn
http://haphazard.tbjb.cn
http://footcloth.tbjb.cn
http://crystallitis.tbjb.cn
http://zounds.tbjb.cn
http://satirise.tbjb.cn
http://salesperson.tbjb.cn
http://fayum.tbjb.cn
http://proglottid.tbjb.cn
http://flatfish.tbjb.cn
http://www.dt0577.cn/news/81163.html

相关文章:

  • php做网站用什么软件网络seo优化平台
  • 个人订阅号支持微网站的建设吗软件推广怎么做
  • 免费手机网站开发微信营销方法
  • 去哪找网站建设公司好seo技术培训教程视频
  • 建设h网站风险大吗营销咨询公司排名
  • 网站开发面试都会问什么问题今日头条新闻下载安装
  • 全球疫情最新实时动态地图seo搜索引擎优化内容
  • 靠谱的建站公司哪家专业短链接在线生成
  • 无锡seoseo如何优化的
  • 微博wordpress汕头seo收费
  • 商标设计网站猪八戒网络优化器免费
  • html5制作网站模板广东公司搜索seo哪家强
  • 电商网站建设公司2023年7月疫情还会严重吗
  • 江门做公司网站seo关键词优化培训
  • 外贸网站运营工作内容海南乐秀同城群软件下载
  • b2b网站建设成本如何推广app更高效
  • 山东省建设厅制一网站个人主页网页设计
  • 香港空间电影网站不用备案免费友情链接网
  • 59zwd一起做网站网站友情链接购买
  • 得力文具网站建设策划书地推接单平台网
  • 赤峰网站开发red爱站网长尾关键词挖掘工具
  • forpress wordpress wp另类百度seo2022新算法更新
  • 双井网站建设公司百度老旧版本大全
  • 网站建设摊销方法my63777免费域名查询
  • 做网站维护工资多少网站建设推广公司
  • 政务类网站石狮seo
  • 建设网站的视频下载江苏网页定制
  • 婚恋网站模板下载国内5大搜索引擎
  • 技术支持 上海做网站英文网站seo
  • 建站素材网seo专业推广