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


边栏推荐
- Leetcode skimming questions_ Sum of squares
- Code Review关注点
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
- Force buckle 1020 Number of enclaves
- Development trend of Ali Taobao fine sorting model
- SPIR-V初窺
- Opinions on softmax function
- Format code_ What does formatting code mean
- [flask] official tutorial -part1: project layout, application settings, definition and database access
- [flask] obtain request information, redirect and error handling
猜你喜欢

Basic operations of databases and tables ----- non empty constraints

A Cooperative Approach to Particle Swarm Optimization

3D模型格式汇总

晶振是如何起振的?

Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步

Leetcode skimming questions_ Invert vowels in a string

ORA-00030

VMware Tools installation error: unable to automatically install vsock driver

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

一图看懂!为什么学校教了你Coding但还是不会的原因...
随机推荐
[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.
Force buckle 9 palindromes
Force buckle 1020 Number of enclaves
Redis-Key的操作
Code review concerns
500 lines of code to understand the principle of mecached cache client driver
ORA-00030
LeetCode 322. Change exchange (dynamic planning)
Basic operations of database and table ----- delete data table
Crawler request module
[detailed] several ways to quickly realize object mapping
Cookie concept, basic use, principle, details and Chinese transmission
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
General operation method of spot Silver
yii中console方法调用,yii console定时任务
Development trend of Ali Taobao fine sorting model
Nmap: network detection tool and security / port scanner
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Accelerating spark data access with alluxio in kubernetes
Electrical data | IEEE118 (including wind and solar energy)