当前位置:网站首页>Selenium waiting mode
Selenium waiting mode
2022-07-06 01:38:00 【Live up to your youth】
summary
stay selenium in , Waiting refers to waiting for the completion of the previous task before executing the next task , Before the previous task is completed , The latter task will always be in a blocking state , It is somewhat similar to transactions in a database . such as , Before locating the element, you need to load DOM.selenium There are three ways to wait : According to wait 、 An implicit wait 、 Smooth waiting .
The way
According to wait
WebDriverWait(driver, timeout).until(method, message)
Show waiting is selenium The imperative process language that customers can use . They allow your code to pause program execution , Or freeze threads , Until the conditions for passing are met . This condition will always be called at a certain frequency , Until the waiting times out . This means that as long as the condition returns a false value , It will keep trying and waiting . This method is also the most commonly used method .
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")
# Set implicit wait
# locator It can be tuples (), aggregate {}, Tabular form
# function presence_of_element_located Indicates the DOM Whether there is an element on
# If html There is no label in p The elements of , Then wait for three seconds and throw an exception
WebDriverWait(driver, timeout=3).until(expected_conditions.presence_of_element_located({By.TAG_NAME, "p"}))
# If html There are tags in p The elements of , Then find this element
element = driver.find_element(By.TAG_NAME, "p")
An implicit wait
driver.implicitly_wait(time_to_wait)
An implicit wait ,WebDriver Poll for a certain amount of time when trying to find any element DOM. If the element doesn't exist , An exception will be thrown after the set time . It is useful when some elements on a web page are not immediately available and take some time to load .
Implicit waiting is telling WebDriver If you poll for one or more elements that are not immediately available DOM A span . The default setting is 0, Disable . Once set up , Implicit waiting is set as the life cycle of the session . in other words , After setting the implicit wait , When performing any operation, such as finding elements , There is a certain time limit , If it goes beyond that time , Will throw an exception .
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# Set implicit wait , The waiting time is 5 second
driver.implicitly_wait(5)
# After setting the implicit wait , If 5 The target is not obtained within seconds DOM, Throw an exception
driver.get("http://www.example.com")
# After setting the implicit wait , If 5 No element was obtained within seconds a, Throw an exception
element = driver.find_element(By.TAG_NAME, "a")
Smooth waiting
WebDriverWait(driver, timeout, poll_frequency, ignored_exceptions).until(method, message)
The fluent wait instance defines the maximum amount of time for the wait condition , And the frequency of the inspection conditions . Users can configure waiting to ignore specific types of exceptions when waiting , For example, when searching for elements on the page 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")
# Set smooth waiting
# The timeout is 10 second , every other 1 Seconds to check whether ElementNotVisibleException,ElementNotSelectableException These two exceptions , If these two exceptions occur , Ignore it
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")
Because it must be synchronized DOM And instructions are quite common , So most clients also come with a set of predefined waiting conditions . seeing the name of a thing one thinks of its function , They are predefined conditions for frequent wait operations . Here are some optional Waiting conditions :


边栏推荐
- Huawei converged VLAN principle and configuration
- 【详细】快速实现对象映射的几种方式
- [flask] official tutorial -part1: project layout, application settings, definition and database access
- 3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
- 【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
- MCU lightweight system core
- leetcode刷题_平方数之和
- selenium 等待方式
- 国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
- How to see the K-line chart of gold price trend?
猜你喜欢
![[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry](/img/94/05b2ff62a8a11340cc94c69645db73.png)
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry

NiO related knowledge (II)

Basic operations of databases and tables ----- default constraints

leetcode刷题_反转字符串中的元音字母
![[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability](/img/43/a8f302eb69beff4037aadda808f886.png)
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability

普通人下场全球贸易,新一轮结构性机会浮出水面

MATLB | real time opportunity constrained decision making and its application in power system

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

Poj2315 football games

Basic operations of database and table ----- delete data table
随机推荐
【Flask】响应、session与Message Flashing
Idea sets the default line break for global newly created files
DOM introduction
Code review concerns
A glimpse of spir-v
【Flask】静态文件与模板渲染
Paddle framework: paddlenlp overview [propeller natural language processing development library]
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
3D模型格式汇总
Mathematical modeling learning from scratch (2): Tools
干货!通过软硬件协同设计加速稀疏神经网络
TrueType字体文件提取关键信息
selenium 元素定位(2)
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
[le plus complet du réseau] | interprétation complète de MySQL explicite
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
How does Huawei enable debug and how to make an image port
leetcode刷题_验证回文字符串 Ⅱ
Huawei converged VLAN principle and configuration