当前位置:网站首页>selenium 等待方式
selenium 等待方式
2022-07-06 01:34:00 【不负韶华ღ】
概述
在selenium中,等待指的是在执行下一个任务之前需要等待前一个任务完成,在前一个任务没有完成之前,后一个任务会一直处于阻塞状体,有点类似于数据库中的事务。比如,在定位元素之前需要加载完DOM。selenium有三种等待方式:显示等待、隐式等待、流畅等待。
方式
显示等待
WebDriverWait(driver, timeout).until(method, message)
显示等待是selenium客户可以使用的命令式过程语言。它们允许您的代码暂停程序执行,或冻结线程,直到满足通过的条件。这个条件会以一定的频率一直被调用,直到等待超时。这意味着只要条件返回一个假值,它就会一直尝试和等待。这种方式也是最常使用的一种方式。
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
driver = webdriver.Chrome()
driver.get("http://www.example.com")
# 设置隐式等待
# locator可以是元组(),集合{},列表形式
# 函数presence_of_element_located表示检查页面的 DOM 上是否存在元素
# 如果html中没有标签为p的元素,则等待三秒后抛出异常
WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located({By.TAG_NAME, "p"}))
# 如果html中存在标签为p的元素,则找到这个元素
element = driver.find_element(By.TAG_NAME, "p")
隐式等待
driver.implicitly_wait(time_to_wait)
隐式等待,WebDriver在试图查找任何元素时在一定时间内轮询DOM。如果该元素不存在,则超过设定的时间后会抛出异常。当网页上的某些元素不是立即可用并且需要一些时间来加载时是很有用的。
隐式等待是告诉WebDriver如果在查找一个或多个不是立即可用的元素时轮询DOM一段时间。默认设置为0,表示禁用。一旦设置好,隐式等待就被设置为会话的生命周期。也就是说,设置隐式等待后,在执行任何操作比如查找元素,都有一定时间的限制,如果超过这段时间,就会抛出异常。
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# 设置隐式等待,等待时间为5秒
driver.implicitly_wait(5)
# 设置隐式等待后,如果5秒内没有获取到目标DOM,则抛出异常
driver.get("http://www.example.com")
# 设置隐式等待后,如果5秒内没有获取到元素a,则抛出异常
element = driver.find_element(By.TAG_NAME, "a")
流畅等待
WebDriverWait(driver, timeout, poll_frequency, ignored_exceptions).until(method, message)
流畅等待实例定义了等待条件的最大时间量,以及检查条件的频率。用户可以配置等待来忽略等待时出现的特定类型的异常,例如在页面上搜索元素时出现的NoSuchElementException。
from selenium import webdriver
from selenium.common import ElementNotVisibleException, ElementNotSelectableException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome()
driver.get("http://www.example.com")
# 设置流畅等待
# 超时时间为10秒,每隔1秒检查一下是否出现ElementNotVisibleException,ElementNotSelectableException这两个异常,如果出现这两个异常,则忽略它
wait = WebDriverWait(driver, timeout=10, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException,
ElementNotSelectableException])
wait.until(expected_conditions.element_to_be_clickable((By.TAG_NAME, "h")))
element = driver.find_element(By.TAG_NAME, "p")
由于必须同步DOM和指令是相当常见的情况,所以大多数客户端还附带一组预定义的等待条件 。顾名思义,它们是为频繁等待操作预定义的条件。下面是一些可选的等待条件:


边栏推荐
- Recommended areas - ways to explore users' future interests
- internship:项目代码所涉及陌生注解及其作用
- Superfluid_ HQ hacked analysis
- Folio.ink 免费、快速、易用的图片分享工具
- Loop structure of program (for loop)
- Hcip---ipv6 experiment
- Who knows how to modify the data type accuracy of the columns in the database table of Damon
- 现货白银的一般操作方法
- [the most complete in the whole network] |mysql explain full interpretation
- 【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
猜你喜欢

ORA-00030
![[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.](/img/3c/ec97abfabecb3f0c821beb6cfe2983.jpg)
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.

Leetcode skimming questions_ Invert vowels in a string

How to see the K-line chart of gold price trend?

National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!

【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化

Leetcode skimming questions_ Verify palindrome string II

Basic operations of databases and tables ----- primary key constraints
![[flask] official tutorial -part1: project layout, application settings, definition and database access](/img/c3/04422e4c6c1247169999dd86b74c05.png)
[flask] official tutorial -part1: project layout, application settings, definition and database access
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping
随机推荐
Luo Gu P1170 Bugs Bunny and Hunter
ctf. Show PHP feature (89~110)
记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题
Redis-Key的操作
ORA-00030
Opinions on softmax function
Loop structure of program (for loop)
Basic operations of databases and tables ----- default constraints
Basic operations of databases and tables ----- non empty constraints
什么是弱引用?es6中有哪些弱引用数据类型?js中的弱引用是什么?
[flask] static file and template rendering
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
[Jiudu OJ 09] two points to find student information
干货!通过软硬件协同设计加速稀疏神经网络
网易智企逆势进场,游戏工业化有了新可能
[机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
How to get the PHP version- How to get the PHP Version?
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
Leetcode 剑指 Offer 59 - II. 队列的最大值
General operation method of spot Silver