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


边栏推荐
- A Cooperative Approach to Particle Swarm Optimization
- Redis-Key的操作
- 普通人下场全球贸易,新一轮结构性机会浮出水面
- 电气数据|IEEE118(含风能太阳能)
- Redis' cache penetration, cache breakdown, cache avalanche
- Leetcode 剑指 Offer 59 - II. 队列的最大值
- A glimpse of spir-v
- Format code_ What does formatting code mean
- Reasonable and sensible
- Unity | two ways to realize facial drive
猜你喜欢

Basic operations of database and table ----- set the fields of the table to be automatically added

Who knows how to modify the data type accuracy of the columns in the database table of Damon

Test de vulnérabilité de téléchargement de fichiers basé sur dvwa

500 lines of code to understand the principle of mecached cache client driver

dried food! Accelerating sparse neural network through hardware and software co design

ORA-00030

Mathematical modeling learning from scratch (2): Tools

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

General operation method of spot Silver

A Cooperative Approach to Particle Swarm Optimization
随机推荐
Redis-列表
现货白银的一般操作方法
Unreal browser plug-in
ORA-00030
Folio. Ink is a free, fast and easy-to-use image sharing tool
Basic operations of databases and tables ----- unique constraints
How to get the PHP version- How to get the PHP Version?
Condition and AQS principle
Spir - V premier aperçu
Folio.ink 免费、快速、易用的图片分享工具
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
A Cooperative Approach to Particle Swarm Optimization
Yii console method call, Yii console scheduled task
竞赛题 2022-6-26
Force buckle 9 palindromes
Huawei converged VLAN principle and configuration
Docker compose配置MySQL并实现远程连接
电气数据|IEEE118(含风能太阳能)
记一个 @nestjs/typeorm^8.1.4 版本不能获取.env选项问题