当前位置:网站首页>What are the waiting methods of selenium
What are the waiting methods of selenium
2022-07-02 09:26:00 【Yisu cloud】
Selenium What are the ways of waiting
This article is about Selenium What are the contents of the waiting mode . Xiaobian thinks it's very practical , So share it for your reference , Let's follow Xiaobian to have a look .
1、 Mandatory waiting (sleep)
from time import sleep sleep(3) # Mandatory waiting 3 second
shortcoming : because Web The speed of loading depends on the hardware being tested 、 Network speed 、 The server Response time and other factors . If the waiting time is too long , It's easy to waste time , If the waiting time is too short, it may result in web You haven't loaded the required location yet element, And an error is reported .
Because the waiting time cannot be determined , Use too much sleep Will affect the running speed , Greatly reduce efficiency , Therefore, it is recommended to use less forced waiting in the test .
2、 An implicit wait ( implicitly_wait)
# An implicit wait 10s driver.implicitly_wait(10)
brief introduction : Implicit waiting is global and targeted at all elements , Set the waiting time such as 10 second , If 10 Seconds to appear , Then continue down , Otherwise, throw it out of order . It can be understood that 10 Within seconds , Keep refreshing to see if the element is loaded .
Use scenarios : Implicit wait only needs to be declared once , It is usually declared after opening the browser . After the statement to the whole drvier The life cycle of , There is no need to repeat the statement . There is a problem with implicit waiting , That is, the program will wait for the whole page to load , In other words, generally you see that the small circle in the browser tab bar doesn't rotate any more , To take the next step , But sometimes the elements the page wants are already loaded , But because of the individual js Things like that are very slow , You still have to wait until the page is complete before you go to the next step .
3、 According to wait ( expected_conditions)
brief introduction : Show that waiting is for an element alone , Set a waiting time such as 5 second , every other 0.5 Check whether it appears every second , If in 5 Seconds before any time , Then continue down , Generally, it is necessary to cooperate with this kind of until() and until_not() Methods used together , Until the set maximum time is exceeded , Then throw a timeout error TimeoutException, Here are some of the most common methods :
1. Judge element Whether or not visible :visibility_of_element_located(locator) ( Visible represents element Non hidden , also element Neither width nor height is equal to 0)
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
# Example
target = EC.visibility_of_element_located(By.ID,'user')
# coordination until() Use ( Wait for the element to be visible )
# 5 Express Maximum timeout , Default in seconds ; 1 Indicates the interval step of detection , While waiting , Every once in a while ( Default 0.5 second ), call until or until_not The method in , Until it comes back True or False.
WebDriverWait(driver, 5, 1).until(EC.visibility_of_element_located(By.ID,'user'))
# coordination until_not() Use ( Wait for the element to be invisible )
WebDriverWait(driver, 5, 1).until_not(EC.visibility_of_element_located(By.ID,'user'))
# Encapsulated as a function in a class
def wait_eleLocated(self, loc, timeout=30, poll_frequency=0.5, model=None):
"""
:param loc: Element location expression ; A tuple type , The way of expression ( Element location type , Element positioning method ), Example :(By.ID, "kw")
:param timeout: Timeout time
:param poll_frequency: Polling frequency
:param model: Wait for failure , Screenshot operation , The function annotation to be expressed in the picture file
:return:None
"""
self.logger.info(f' wait for "{model}" Elements , Positioning mode :{loc}')
try:
start = datetime.now()
WebDriverWait(self.driver, timeout, poll_frequency).until(EC.visibility_of_element_located(loc))
end = datetime.now()
self.logger.info(f' wait for "{model}" Duration :{end - start}')
except TimeoutException:
self.logger.exception(f' wait for "{model}" Element failed , Positioning mode :{loc}')
# Screenshot
self.save_webImgs(f" Waiting elements [{model}] Something unusual happened ")
raise2. Judge a certain element Whether it is loaded into dom Trees :presence_of_element_located(locator) ( It doesn't mean that element It must be visible )
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By target = EC.presence_of_element_located(By.ID,'user')
3. Judge a certain element Is visible and clickable in :element_to_be_clickable(locator)
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By target = EC.element_to_be_clickable(By.ID,'user')
4. Judge a certain element Is it selected :element_to_be_selected(element) ( Usually used in drop-down list )
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
element = driver.find_element_by_class_name('selector')
EC.element_to_be_selected(element)Thank you for reading ! About “Selenium What are the ways of waiting ” This article is shared here , I hope the above contents can be of some help to you , So that everyone can learn more , If you think the article is good , You can share it and let more people see it !
边栏推荐
- ClassFile - Attributes - Code
- 微服务实战|手把手教你开发负载均衡组件
- 微服务实战|原生态实现服务的发现与调用
- Matplotlib剑客行——容纳百川的艺术家教程
- oracle修改数据库字符集
- 别找了,Chrome浏览器必装插件都在这了
- 微服务实战|微服务网关Zuul入门与实战
- Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
- Microservice practice | declarative service invocation openfeign practice
- 使用IBM MQ远程连接时报错AMQ 4043解决思路
猜你喜欢

数构(C语言)——第四章、矩阵的压缩存储(下)

View the port of the application published by was
![[go practical basis] how to install and use gin](/img/0d/3e899bf69abf4e8cb7e6a0afa075a9.png)
[go practical basis] how to install and use gin

Matplotlib swordsman - a stylist who can draw without tools and code

Servlet全解:继承关系、生命周期、容器和请求转发与重定向等

西瓜书--第六章.支持向量机(SVM)

Typeerror: X () got multiple values for argument 'y‘

DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!

Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
![[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the](/img/dc/c0ea188ef353ded86759dbe9b29df3.jpg)
[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the
随机推荐
Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
自定義Redis連接池
MySql报错:unblock with mysqladmin flush-hosts
VIM operation command Encyclopedia
Sentinel reports failed to fetch metric connection timeout and connection rejection
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
There is a problem with MySQL installation (the service already exists)
Break the cocoon | one article explains what is the real cloud primordial
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
微服务实战|Eureka注册中心及集群搭建
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
Redis 序列化 GenericJackson2JsonRedisSerializer和Jackson2JsonRedisSerializer的区别
Redis安装部署(Windows/Linux)
队列管理器running状态下无法查看通道
深入剖析JVM是如何执行Hello World的
概念到方法,绝了《统计学习方法》——第三章、k近邻法
Avoid breaking changes caused by modifying constructor input parameters
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
「Redis源码系列」关于源码阅读的学习与思考
WSL安装、美化、网络代理和远程开发