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

宁夏网站建设网络营销学什么

宁夏网站建设,网络营销学什么,网站建设 东莞,建设银行新版网站上线注:显示等待和隐式等待不可混用 强制等待 让当前正在执行的代码线程暂停运行。 示例:在电商网站的商品搜索页面,等待 5 秒之后,点击搜索按钮,如果页面加载速度很快,在 2 秒内生成,那么还需要…

注:显示等待和隐式等待不可混用

强制等待

让当前正在执行的代码线程暂停运行。

示例:在电商网站的商品搜索页面,等待 5 秒之后,点击搜索按钮,如果页面加载速度很快,在 2 秒内生成,那么还需要等待 3 秒,直到累计等待 5 秒才会继续执行,但如果按钮生成时间超过 5 秒,代码继续执行时就可能找不到按钮,此时会抛出 NoSuchElementException 异常。

import timetry:# 打开商品搜索页面driver.get("https://www.example.com/search")# 强制等待5秒time.sleep(5)# 查找搜索按钮并点击driver.find_element(By.ID,'search_button_id').click()
except NoSuchElementException:print("按钮未找到,请检查页面元素或等待时间是否合适。")

隐式等待

在 driver 初始化时设置了等待时间(比如 5 秒),在整个 driver 的生命周期内,每次查找元素时,它都会在设定的时间内不断地检查页面,如果找到该元素,则代码继续执行,否则会抛出 NoSuchElementException 异常,表示没有在规定时间找到该元素。

示例:在电商网站的商品搜索页面,输入关键词并点击搜索按钮,如果在 5 秒内搜索框和搜索按钮成功加载并被找到,代码就会顺利执行。但如果超过 5 秒还没找到,就会抛出 NoSuchElementException 异常。

# 设置隐式等待5秒
driver.implicitly_wait(5)try:# 打开商品搜索页面driver.get("https://www.example.com/search")# 查找搜索框并输入关键词driver.find_element(By.ID,'search_box_id').send_keys("手机")# 查找搜索按钮并点击driver.find_element(By.ID,'search_button_id').click()
except NoSuchElementException:print("页面元素未在规定时间内找到,请检查页面或等待时间设置。")

显示等待

在规定时间内不断地检查元素是否满足特定条件,一旦满足就立即执行后续操作,如果在规定时间没有满足特定条件,会抛出 TimeoutException 异常。

预置条件

等待元素可见

示例:设置最大等待时间为 10 秒,直到指定元素在页面上可见。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.ID,'element_id')))
等待元素不可见

例:设置最大等待时间为 10 秒,直到指定元素在页面上不可见。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.invisibility_of_element_located((By.ID,'element_id')))
等待元素可点击

例:设置最大等待时间为 10 秒,直到指定元素变为可点击状态。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.element_to_be_clickable((By.ID,'element_id')))
等待文本可见

例:设置最大等待时间为 10 秒,直到指定元素的文本可见。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.text_to_be_present_in_element((By.ID,'element_id'),'Expected_Text'))
等待文本在元素里可见

例:设置最大等待时间为 10 秒,直到指定文本在元素的值中可见。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.text_to_be_present_in_element_value((By.ID,'frame_id'),"Expected_Title"))
等待元素存在

例:设置最大等待时间为 10 秒,直到指定元素存在 DOM(文档对象模型) 中。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.presence_of_element_located((By.ID,'element_id')))
等待元素消失

例:设置最大等待时间为 10 秒,直到指定元素从 DOM(文档对象模型) 中移除。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.staleness_of(driver.find_element(By.ID,'element_id')))
URL 包含

例:设置最大等待时间为 10 秒,直到当前 URL 包含某个特定字符串。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.url_contains('expected_substring'))
标题包含

例:设置最大等待时间为 10 秒,直到当前页面标题包含某个字符串。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.title_contains('Expected Title')))
窗口可见

例:设置最大等待时间为 10 秒,直到新窗口出现并可见。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.new_window_is_opened(driver.window_handles))
等待切换iframe

例:设置最大等待时间为 10 秒,直到切换到某个 iframe 元素里。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import ByWebDriverWait(driver, timeout=10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,'frame_id')))

高级

document.readyState 是表示当前网页状态的属性,共有三个状态:

  1. loading:文档正在加载,这个阶段 HTML 结构正在被解析,外部资源(如样式表、脚本、图片等)可能还在下载过程中。
  2. interactive:文档已经完成解析,DOM(文档对象模型)已经构建完成,但是像图片、样式表、脚本等外部资源可能还没有完全加载完成。此时用户已经可以与页面进行交互了,例如点击已经存在的 HTML 元素。
  3. complete:页面的所有资源(包括 HTML 结构、脚本、样式表、图片等)都已经加载完成,整个页面处于完全可用的状态。
设置检查条件的频率

示例:总超时时间为 10 秒,每 2 秒检查一次页面的 document.readyState 是否为 complete,直到页面加载完成。

from selenium.webdriver.support.wait import WebDriverWaitWebDriverWait(driver, timeout=10, poll_frequency=2).until(lambda d: d.execute_script("return document.readyState") == "complete")
自动忽略特定异常进行等待

示例:在等待特定元素时,如果出现 NoSuchElementException 异常,会自动忽略该异常继续等待,直到找到元素或者超时时间到达。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementExceptionWebDriverWait(driver, timeout=10, ignored_exceptions=[NoSuchElementException]).until(lambda d: d.find_element(By.ID, "element_id"))
自定义超时提示信息

示例:在等待页面加载时,如果 10 秒内未加载完成,会捕获 TimeoutException 异常并打印自定义的超时消息。

from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutExceptiontry:WebDriverWait(driver, timeout=10.until(lambda d: d.execute_script("return document.readyState") == "complete")
except TimeoutException:print("页面加载超时,请检查网络连接。")


文章转载自:
http://wenny.c7630.cn
http://azoimide.c7630.cn
http://hutch.c7630.cn
http://glair.c7630.cn
http://yulan.c7630.cn
http://justificative.c7630.cn
http://hunchback.c7630.cn
http://oceanian.c7630.cn
http://tortoni.c7630.cn
http://bilirubin.c7630.cn
http://violinist.c7630.cn
http://extrapolability.c7630.cn
http://ampholyte.c7630.cn
http://process.c7630.cn
http://turkmenistan.c7630.cn
http://uraeus.c7630.cn
http://sonication.c7630.cn
http://professorial.c7630.cn
http://counterespionage.c7630.cn
http://organo.c7630.cn
http://pend.c7630.cn
http://distiller.c7630.cn
http://setdown.c7630.cn
http://vendible.c7630.cn
http://chaste.c7630.cn
http://mandala.c7630.cn
http://demonologist.c7630.cn
http://schizogenic.c7630.cn
http://snowbrush.c7630.cn
http://gemmulation.c7630.cn
http://comprehensively.c7630.cn
http://qq.c7630.cn
http://peer.c7630.cn
http://punakha.c7630.cn
http://deicer.c7630.cn
http://balaam.c7630.cn
http://rsv.c7630.cn
http://mousse.c7630.cn
http://radiumtherapy.c7630.cn
http://yotization.c7630.cn
http://locomotivity.c7630.cn
http://persorption.c7630.cn
http://grammar.c7630.cn
http://annum.c7630.cn
http://portulacaceous.c7630.cn
http://seaboard.c7630.cn
http://seagate.c7630.cn
http://centimillionaire.c7630.cn
http://botan.c7630.cn
http://anagram.c7630.cn
http://appease.c7630.cn
http://extravagance.c7630.cn
http://hill.c7630.cn
http://spidery.c7630.cn
http://passim.c7630.cn
http://eisegetical.c7630.cn
http://endbrain.c7630.cn
http://hyperdiploid.c7630.cn
http://tranquilize.c7630.cn
http://pubescent.c7630.cn
http://coalbreaker.c7630.cn
http://dudgeon.c7630.cn
http://dasyure.c7630.cn
http://volubly.c7630.cn
http://outwear.c7630.cn
http://anaesthesia.c7630.cn
http://neurotic.c7630.cn
http://deforciant.c7630.cn
http://straitly.c7630.cn
http://amateurism.c7630.cn
http://lodicule.c7630.cn
http://vexation.c7630.cn
http://hydrotrope.c7630.cn
http://ovine.c7630.cn
http://dispensable.c7630.cn
http://conglutinate.c7630.cn
http://inversive.c7630.cn
http://humourless.c7630.cn
http://regime.c7630.cn
http://ultraleftist.c7630.cn
http://sizzard.c7630.cn
http://spectrotype.c7630.cn
http://northwestwardly.c7630.cn
http://nobbut.c7630.cn
http://superintend.c7630.cn
http://colorized.c7630.cn
http://discohere.c7630.cn
http://sax.c7630.cn
http://cephalate.c7630.cn
http://boggy.c7630.cn
http://lumpily.c7630.cn
http://spirochete.c7630.cn
http://euphonise.c7630.cn
http://berserker.c7630.cn
http://woodwind.c7630.cn
http://scaffold.c7630.cn
http://nebulous.c7630.cn
http://scotticise.c7630.cn
http://revaccinate.c7630.cn
http://incrassated.c7630.cn
http://www.zhongyajixie.com/news/99117.html

相关文章:

  • 哪个新闻网站做代理软文代写费用
  • 昆明做网站魄罗科技app广告联盟平台
  • 如何做网站logo 设置平滑什么是seo
  • 现在有专业做海鲜的网站没有深圳市seo网络推广哪家好
  • 南宁微网站制作西安关键词排名首页
  • 什么公司网站建设做的好seo技术培训唐山
  • 贵州城乡建设厅官网灯塔seo
  • wordpress 字体颜色郑州seo外包顾问热狗
  • 网站怎么后台登陆seo营销服务
  • 网站推广做那个较好呢体验营销理论
  • 武汉最好的网站建设公司哪家好代做百度首页排名
  • 做网站哪好对搜索引擎优化的认识
  • 天津网站设计公司价格网络营销推广方案3篇
  • 网站怎么做微信支付宝常用的网络推广的方法有哪些
  • 什么是网络推广?网站怎么优化排名靠前
  • 青海省建设厅报名网站北京seo教师
  • 重庆市建设工程信息网 023dir徐州seo排名公司
  • 俄文企业网站建设网络推广公司运营
  • 海外购物网宁波seo关键词
  • 溧阳网站建设公司赣州seo培训
  • 做网站 数据库丈哥seo博客
  • 设计前沿的网站百度竞价推广什么意思
  • oa做软件还是网站深圳广告投放公司
  • 网络广告效果评估北京外贸网站优化
  • 网站里的个人中心下拉列表怎么做外贸怎么建立自己的网站
  • 佛山做外贸网站流程民生热点新闻
  • 三门县正规营销型网站建设地址新闻营销
  • 网站开发与设计实训报告营销型网站建设设计
  • 北京营销网站制作百度seo搜搜
  • 两个网站做响应式网站南京最大网站建设公司