当前位置:网站首页>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和指令是相当常见的情况,所以大多数客户端还附带一组预定义的等待条件 。顾名思义,它们是为频繁等待操作预定义的条件。下面是一些可选的等待条件:


边栏推荐
- Yii console method call, Yii console scheduled task
- [network attack and defense training exercises]
- How does Huawei enable debug and how to make an image port
- 【已解决】如何生成漂亮的静态文档说明页
- 【网络攻防实训习题】
- [机缘参悟-39]:鬼谷子-第五飞箝篇 - 警示之二:赞美的六种类型,谨防享受赞美快感如同鱼儿享受诱饵。
- Redis' cache penetration, cache breakdown, cache avalanche
- Code Review关注点
- Leetcode1961. Check whether the string is an array prefix
- Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
猜你喜欢

VMware Tools installation error: unable to automatically install vsock driver

leetcode刷题_平方数之和

leetcode刷题_验证回文字符串 Ⅱ

UE4 unreal engine, editor basic application, usage skills (IV)

【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库

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

现货白银的一般操作方法

一圖看懂!為什麼學校教了你Coding但還是不會的原因...

MUX VLAN configuration

Basic operations of database and table ----- set the fields of the table to be automatically added
随机推荐
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
File upload vulnerability test based on DVWA
Unity VR resource flash surface in scene
c#网页打开winform exe
internship:项目代码所涉及陌生注解及其作用
Loop structure of program (for loop)
Electrical data | IEEE118 (including wind and solar energy)
PHP error what is an error?
Unreal browser plug-in
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
3D模型格式汇总
Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
Opinions on softmax function
037 PHP login, registration, message, personal Center Design
Development trend of Ali Taobao fine sorting model
500 lines of code to understand the principle of mecached cache client driver
What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
leetcode刷题_反转字符串中的元音字母
Basic operations of databases and tables ----- primary key constraints
Paging of a scratch (page turning processing)