当前位置:网站首页>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和指令是相当常见的情况,所以大多数客户端还附带一组预定义的等待条件 。顾名思义,它们是为频繁等待操作预定义的条件。下面是一些可选的等待条件:
边栏推荐
猜你喜欢
Basic operations of databases and tables ----- primary key constraints
VMware Tools installation error: unable to automatically install vsock driver
ORA-00030
Who knows how to modify the data type accuracy of the columns in the database table of Damon
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
Unity | two ways to realize facial drive
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
MUX VLAN configuration
Cookie concept, basic use, principle, details and Chinese transmission
随机推荐
Folio.ink 免费、快速、易用的图片分享工具
MySQL learning notes 2
网易智企逆势进场,游戏工业化有了新可能
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
Paging of a scratch (page turning processing)
【Flask】响应、session与Message Flashing
ThreeDPoseTracker项目解析
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
剑指 Offer 38. 字符串的排列
How to see the K-line chart of gold price trend?
[detailed] several ways to quickly realize object mapping
Redis' cache penetration, cache breakdown, cache avalanche
JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
WGet: command line download tool
Test de vulnérabilité de téléchargement de fichiers basé sur dvwa
[flask] obtain request information, redirect and error handling
Tcpdump: monitor network traffic
Redis-字符串类型
伦敦银走势中的假突破
Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization