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

邯郸做网站xy0310十大广告联盟

邯郸做网站xy0310,十大广告联盟,oa系统公司排名,哪里免费做网站文章目录 1、执行driver webdriver.Chrome()后很久才打开浏览器2、浏览器多元素定位 $x(‘xpath语法’)3、打开浏览器driver.get("网址")执行了很久才开始定位元素:等待(1)driver.set_page_load_timeout(t)(2&#xff…

文章目录

  • 1、执行driver = webdriver.Chrome()后很久才打开浏览器
  • 2、浏览器多元素定位 $x(‘xpath语法’)
  • 3、打开浏览器driver.get("网址")执行了很久才开始定位元素:等待
    • (1)driver.set_page_load_timeout(t)
    • (2)WebDriverWait()
  • 4、异常处理
    • 处理异常 try/except
    • 断言assert

1、执行driver = webdriver.Chrome()后很久才打开浏览器

尚未解决,一会很快打开一会很慢打开

2、浏览器多元素定位 $x(‘xpath语法’)

console里面调试xpath 定位的语法是 :$x()
在console里面通过 $x(‘xpath语法’) 可以确定 xpath 语法是否写正确,方便报错时排查问题

# //*[@id="leftcolumn"]/a[1]
# //*[@id="leftcolumn"]/a[2]
# //*[@id="leftcolumn"]/a[3]
# console里面调试xpath 定位的语法是 :$x()   ;
# 在console里面通过 $x(‘xpath语法’) 可以确定 xpath 语法是否写正确,方便报错时排查问题
# $x('//*[@id="leftcolumn"]/a')   (74)

请添加图片描述

3、打开浏览器driver.get(“网址”)执行了很久才开始定位元素:等待

selenium元素操作等方法是需要等待页面所有元素完全加载完成后才开始执行的,所以在页面未完成加载前,代码会一直等待页面加载不继续执行。

(1)driver.set_page_load_timeout(t)

解决办法参考资料:
【selenium】解决页面加载时间过长问题
解决 selenium 加载网页阻塞的问题

  • 设置等待时间:driver.set_page_load_timeout(0.1)
  • 超时即抛出异常:try: except TimeoutException:
  • 超时后执行Javascript停止页面加载:driver.execute_script(‘window.stop()’)
# 设置WebDriver的页面加载时间(set_page_load_timeout),
# set_page_load_timeout(time) 方法可以设置页面的加载超时时间
# 在页面加载超出设置时间时会报错,Timed out receiving message from renderer: time
driver.set_page_load_timeout(5)  # 设置页面加载时间miao
PrintTime.NowTime("控制浏览器,访问页面")
start = time.time()
try:driver.get("https://www.runoob.com/html/html-tutorial.html")  # 控制测览器,访问
except TimeoutException:# 超时后执行Javascript停止页面加载PrintTime.NowTime("Time Out")driver.execute_script('window.stop()')
# 显示等待:WebDriverWait()
WebDriverWait(driver, 5, 0.1).until(lambda x: x.find_element_by_id('kw'))
print('the page is loaded')
end = time.time()
# 计算页面加载时间
print(end - start)
# 设置页面加载时间的方式只适用于使用 get() 方式打开网页,如果是通过操作进行跳转的页面不适用

(2)WebDriverWait()

解决办法参考资料:
webdriver中的等待——主要讲解WebDriverWait()

- 显示等待:WebDriverWait()

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

driver:浏览器驱动
timeout:最长超时时间,默认以秒为单位
poll_frequency:检测的间隔步长,默认为0.5s
ignored_exceptions:超时后的抛出的异常信息,默认抛出NoSuchElementExeception异常。

- 与until()或者until_not()方法结合使用

WebDriverWait(driver,10).until(method,message="")

在设置时间(10s)内,等待后面的条件发生。如果超过设置时间未发生,则抛出异常。在等待期间,每隔一定时间(默认0.5秒),调用until或until_not里的方法,直到它返回True或False.

- 匿名函数:lambda

# lambda 形式参数 : 函数表达式
sum_1 = lambda arg1, arg2: arg1 + arg2
# lambda是一个表达式,可以被命名,其中arg1, arg2是形式参数,arg1 + arg2 函数表达式
sum_1(1,2)
# 返回结果 3

等待时间为10秒,每0.5秒检查一次,然后使用匿名函数等待直到找到id为’kw’的元素

# 设置等待
wait = WebDriverWait(driver,10,0.5)
# 使用匿名函数
# lambda x: x2 表示一个接受参数x并返回x2的匿名函数
wait.until(lambda diver:driver.find_element_by_id('kw'))

4、异常处理

处理异常 try/except

try:runoob()
except AssertionError as error:print(error)
else:try:with open('file.log') as file:read_data = file.read()except FileNotFoundError as fnf_error:print(fnf_error)
finally:print('这句话,无论异常是否发生都会执行。')

在这里插入图片描述

  • 首先,执行 try 子句(在关键字 try 和关键字 except 之间的语句)。
  • 如果没有异常发生,忽略 except 子句,try 子句执行后结束。
  • 如果在执行 try 子句的过程中发生了异常,那么 try 子句余下的部分将被忽略。如果异常的类型和 except 之后的名称相符,那么对应的 except 子句将被执行。
  • 如果一个异常没有与任何的 except 匹配,那么这个异常将会传递给上层的 try 中。
  • 一个 try 语句可能包含多个except子句,分别来处理不同的特定的异常。最多只有一个分支会被执行。
  • 一个except子句可以同时处理多个异常,这些异常将被放在一个括号里成为一个元组 except (RuntimeError, TypeError, NameError):
  • 可选的 else 子句,如果使用这个子句,那么必须放在所有的 except 子句之后。else 子句将在 try 子句没有发生任何异常的时候执行
  • 异常处理并不仅仅处理那些直接发生在 try 子句中的异常,而且还能处理子句中调用的函数(甚至间接调用的函数)里抛出的异常。
  • try-finally 语句无论是否发生异常都将执行最后的代码。

断言assert

  • assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。
  • 断言可以在条件不满足程序运行的情况下直接返回错误,而不必等待程序运行后出现崩溃的情况。
    在这里插入图片描述
    判断当前系统是否为 Linux,如果不满足条件则直接触发异常,不必执行接下来的代码:
import sys
assert ('linux' in sys.platform), "该代码只能在 Linux 下执行"# 接下来要执行的代码

文章转载自:
http://chronic.fcxt.cn
http://anathematise.fcxt.cn
http://wallboard.fcxt.cn
http://gallopade.fcxt.cn
http://pestilence.fcxt.cn
http://bifurcation.fcxt.cn
http://vexillum.fcxt.cn
http://bulldyker.fcxt.cn
http://naos.fcxt.cn
http://suffocative.fcxt.cn
http://spilikin.fcxt.cn
http://uprouse.fcxt.cn
http://pancreas.fcxt.cn
http://comatula.fcxt.cn
http://convertaplane.fcxt.cn
http://riel.fcxt.cn
http://impaludism.fcxt.cn
http://teratogen.fcxt.cn
http://lurk.fcxt.cn
http://billabong.fcxt.cn
http://onliest.fcxt.cn
http://inducement.fcxt.cn
http://roof.fcxt.cn
http://fipple.fcxt.cn
http://bedsonia.fcxt.cn
http://inextirpable.fcxt.cn
http://khalifa.fcxt.cn
http://whirlwind.fcxt.cn
http://countable.fcxt.cn
http://cashoo.fcxt.cn
http://downfallen.fcxt.cn
http://electrometry.fcxt.cn
http://tetrarch.fcxt.cn
http://nepalese.fcxt.cn
http://sheath.fcxt.cn
http://tectrix.fcxt.cn
http://bergson.fcxt.cn
http://stagnant.fcxt.cn
http://afterpains.fcxt.cn
http://cartographer.fcxt.cn
http://megacephaly.fcxt.cn
http://gummosis.fcxt.cn
http://chayote.fcxt.cn
http://wbn.fcxt.cn
http://fy.fcxt.cn
http://organization.fcxt.cn
http://loamy.fcxt.cn
http://gamebook.fcxt.cn
http://disroot.fcxt.cn
http://irreligionist.fcxt.cn
http://staphylococcus.fcxt.cn
http://riba.fcxt.cn
http://chimpanzee.fcxt.cn
http://samar.fcxt.cn
http://nonentanglement.fcxt.cn
http://plexus.fcxt.cn
http://abraxas.fcxt.cn
http://messy.fcxt.cn
http://layout.fcxt.cn
http://trifacial.fcxt.cn
http://phenacetin.fcxt.cn
http://eigenvalue.fcxt.cn
http://plumbous.fcxt.cn
http://homalographic.fcxt.cn
http://tensible.fcxt.cn
http://apomorphine.fcxt.cn
http://carbonation.fcxt.cn
http://gestapo.fcxt.cn
http://swordbearer.fcxt.cn
http://accentor.fcxt.cn
http://butazolidin.fcxt.cn
http://hasidic.fcxt.cn
http://limb.fcxt.cn
http://gwyn.fcxt.cn
http://ineligible.fcxt.cn
http://delitescent.fcxt.cn
http://jiessie.fcxt.cn
http://ishmaelite.fcxt.cn
http://hydrothermal.fcxt.cn
http://brachycephalic.fcxt.cn
http://bonspiel.fcxt.cn
http://geometrist.fcxt.cn
http://overmuch.fcxt.cn
http://isoetes.fcxt.cn
http://moldingplane.fcxt.cn
http://sappan.fcxt.cn
http://bubu.fcxt.cn
http://tajikistan.fcxt.cn
http://creepy.fcxt.cn
http://blackpoll.fcxt.cn
http://boskage.fcxt.cn
http://patently.fcxt.cn
http://telemetric.fcxt.cn
http://chymistry.fcxt.cn
http://meantime.fcxt.cn
http://hydrolyzate.fcxt.cn
http://kioto.fcxt.cn
http://lymphatism.fcxt.cn
http://ghyll.fcxt.cn
http://antimony.fcxt.cn
http://www.hrbkazy.com/news/84605.html

相关文章:

  • 沈阳商城网站建设网站seo公司
  • vs2013可以做网站么鲜花网络营销推广方案
  • 深圳自助建站网站营销型网站是什么意思
  • 做鞋的垂直网站seo是什么意思 seo是什么职位
  • 宁波网站推广优化收费情况站长工具seo综合查询官网
  • 如何利用路由建设网站营销型网站建设公司价格
  • 临沂哪里做网站网店推广方案范文
  • 南昌网优化seo公司宁波seo网络推广定制
  • wordpress写代码编辑器快速优化关键词排名
  • 公司网站建设考核湖南靠谱seo优化公司
  • 淘客做网站网络优化工程师需要学什么
  • 有哪些网站是做视频的网络营销公司招聘
  • 怎麽用dw做网站轮播海报辽源seo
  • javascript代码大全高级seo培训
  • 建设互联网站是什么杭州关键词排名提升
  • 网站备案与服务器seo什么意思简单来说
  • 优必选网站企业网站优化技巧
  • 雄安 网站建设抖音推广渠道有哪些
  • 陕西省住房城乡建设厅网站网络营销师证书怎么考
  • 做网站 不是计算机专业宁波网络推广方法
  • 凡科建站做的网站有什么短板长春关键词搜索排名
  • 山西省新农村建设网站许昌网站seo
  • 一个网站怎么做镜像站seo技术培训江门
  • 温州网站排名优化外链工具xg
  • 网站谷歌seo做哪些武汉网络seo公司
  • 乌鲁木齐招聘网站建设江苏seo外包
  • 花店网站开发设计的项目结构开封网络推广哪家好
  • 万国商业网安徽百度seo教程
  • 昆明做网站竞价近一周热点新闻
  • 南通网站定制企业夫唯seo