当前位置:网站首页>Automated test series selenium three kinds of waiting for detailed explanation
Automated test series selenium three kinds of waiting for detailed explanation
2022-07-25 00:23:00 【Test kid】
The first and simplest way is to force waiting sleep(time), Compulsory transfer procedure, etc time Second time , Whether or not the program can keep up with the speed , Or has it arrived ahead of time , We all have to wait time Duration .
The following code case shows :
from selenium import webdriverfrom time
import sleepdriver = webdriver.Chrome()
driver.get('http://www.1000phone.com')
sleep(5) # Mandatory waiting 5 Seconds before proceeding to the next step
print(driver.current_url)driver.quit()This is called forced waiting , Whether your browser is loaded or not , The program has to wait 5 second ,5 One second to , Continue with the following code , Useful as a debugging tool , Sometimes you can wait like this in the code , However, it is not recommended to always wait in this way , Too rigid , Seriously affect the execution speed of the program .
Two . An implicit wait
The second is implicit waiting ,implicitly_wait(time) The meaning of implicit waiting is : Sun Wukong and Yang Jian agreed , Wherever the monkey king goes , We have to wait for Yang Jian time second , If Yang Jian comes during this time , Then they immediately set out to fight monsters , If Yang Jian doesn't arrive within the specified time , Then the monkey king went by himself , Naturally, let's wait for Yang Jian to throw the exception .
As shown in the following code :
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()driver.implicitly_wait(10)# An implicit wait , The longest wait 10 second driver.get('http://www.1000phone.com')
print(driver.current_url)
driver.quit()Implicit waiting is setting a maximum waiting time , If the web page is loaded within the specified time , Then go to the next step , Otherwise, wait until the deadline , Then go to the next step .
Note that there is a drawback , 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 to go to the next step .
I want to wait until the elements I want come out and what to do next ? Is there a way to , It depends on selenium Another way to wait —— Explicit waiting WebDriverWait 了 . One thing to note is that : Implicit wait for the whole driver All the cycles work , So just set it once , I've seen people treat implicit waiting as sleep In use , Come wherever you go .
3、 ... and . Explicit waiting
The third is to explicitly wait ,WebDriverWait, With this kind of until() and until_not() Method , We can wait flexibly according to the judging conditions .
Its main meaning is : Program every time Take a second , If the conditions hold , Then go to the next step , Otherwise, keep waiting , Until the set maximum time is exceeded , Then throw TimeoutException.
As shown in the following code :
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# Implicit waiting and explicit waiting can be used at the same time , But should pay attention to : The longest waiting time is the greater of the two
driver.implicitly_wait(10) driver.get('http://www.1000phone.com')
locator = (By.LINK_TEXT, ' Discipline ')
try:
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator)) print(driver.find_element_by_link_text(' Discipline ').get_attribute('href'))
finally:
driver.close()We set implicit wait and explicit wait , In other operations , Implicit waiting plays a decisive role , stay WebDriverWait... Explicit waiting plays a major role in , But here's the thing : The longest waiting time depends on the larger of the two , As in the above example 20, If the implicit wait time > Explicit wait time , Then the maximum waiting time of the sentence code is equal to the implicit waiting time .
Explicitly wait for the method to be called :
WebDriverWait(driver, Time out period , Call frequency , Ignore exceptions ).until( Executable method , Information returned on timeout )
Here is the special note until or until_not The executable method in method Parameters
So let's take a look at selenium What are the conditions provided :expected_conditions yes selenium A module of , It contains a series of conditions that can be used to judge :
selenium.webdriver.support.expected_conditions( modular )
1. Verify the parameters passed in title Whether equal to or contained in driver.title
title_is
title_containsAutomated test series -Selenium Three kinds of waiting for detailed explanation
2022-07-24 20:53· Test waiter
One . Mandatory waiting sleep
The first and simplest way is to force waiting sleep(time), Compulsory transfer procedure, etc time Second time , Whether or not the program can keep up with the speed , Or has it arrived ahead of time , We all have to wait time Duration .
The following code case shows :
from selenium import webdriverfrom time
import sleepdriver = webdriver.Chrome()
driver.get('http://www.1000phone.com')
sleep(5) # Mandatory waiting 5 Seconds before proceeding to the next step
print(driver.current_url)driver.quit()This is called forced waiting , Whether your browser is loaded or not , The program has to wait 5 second ,5 One second to , Continue with the following code , Useful as a debugging tool , Sometimes you can wait like this in the code , However, it is not recommended to always wait in this way , Too rigid , Seriously affect the execution speed of the program .
Two . An implicit wait
The second is implicit waiting ,implicitly_wait(time) The meaning of implicit waiting is : Sun Wukong and Yang Jian agreed , Wherever the monkey king goes , We have to wait for Yang Jian time second , If Yang Jian comes during this time , Then they immediately set out to fight monsters , If Yang Jian doesn't arrive within the specified time , Then the monkey king went by himself , Naturally, let's wait for Yang Jian to throw the exception .
As shown in the following code :
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()driver.implicitly_wait(10)# An implicit wait , The longest wait 10 second driver.get('http://www.1000phone.com')
print(driver.current_url)
driver.quit()Implicit waiting is setting a maximum waiting time , If the web page is loaded within the specified time , Then go to the next step , Otherwise, wait until the deadline , Then go to the next step .
Note that there is a drawback , 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 to go to the next step .
I want to wait until the elements I want come out and what to do next ? Is there a way to , It depends on selenium Another way to wait —— Explicit waiting WebDriverWait 了 . One thing to note is that : Implicit wait for the whole driver All the cycles work , So just set it once , I've seen people treat implicit waiting as sleep In use , Come wherever you go .
3、 ... and . Explicit waiting
The third is to explicitly wait ,WebDriverWait, With this kind of until() and until_not() Method , We can wait flexibly according to the judging conditions .
Its main meaning is : Program every time Take a second , If the conditions hold , Then go to the next step , Otherwise, keep waiting , Until the set maximum time is exceeded , Then throw TimeoutException.
As shown in the following code :
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# Implicit waiting and explicit waiting can be used at the same time , But should pay attention to : The longest waiting time is the greater of the two
driver.implicitly_wait(10) driver.get('http://www.1000phone.com')
locator = (By.LINK_TEXT, ' Discipline ')
try:
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator)) print(driver.find_element_by_link_text(' Discipline ').get_attribute('href'))
finally:
driver.close()We set implicit wait and explicit wait , In other operations , Implicit waiting plays a decisive role , stay WebDriverWait... Explicit waiting plays a major role in , But here's the thing : The longest waiting time depends on the larger of the two , As in the above example 20, If the implicit wait time > Explicit wait time , Then the maximum waiting time of the sentence code is equal to the implicit waiting time .
Explicitly wait for the method to be called :
WebDriverWait(driver, Time out period , Call frequency , Ignore exceptions ).until( Executable method , Information returned on timeout )
Here is the special note until or until_not The executable method in method Parameters
So let's take a look at selenium What are the conditions provided :expected_conditions yes selenium A module of , It contains a series of conditions that can be used to judge :
selenium.webdriver.support.expected_conditions( modular )
1. Verify the parameters passed in title Whether equal to or contained in driver.title
title_is
title_contains2. Verify that the element appears , The parameters passed in are of tuple type locator, Such as (By.ID, ‘kw’)
presence_of_element_located
presence_of_all_elements_located3. Verify that the element is visible , The first two incoming parameters are of tuple type locator, The third one came in WebElement The first and the third are essentially the same
visibility_of_element_located
invisibility_of_element_located
visibility_of4. Determine whether a piece of text appears in an element , Of a judging element text, Of a judging element value
text_to_be_present_in_element
text_to_be_present_in_element_valuecondition, And until、until_not A combination can make a lot of judgments , If it can be flexibly packaged by itself , Will greatly improve the stability of the script .
Finally, in my QQ The technical exchange group sorted out my 10 Some technical data sorted out by software testing career in recent years , Include : e-book , Resume module , Various work templates , Interview treasure , Self study projects, etc . If you encounter problems in your study or work , There will also be great gods in the group to help solve , Group number 798478386 ( remarks CSDN555 )
Full set of software test automation test teaching video

300G Download tutorial materials 【 Video tutorial +PPT+ Project source code 】

A complete set of software testing automation testing factory has been

边栏推荐
- Weekly summary (*66): next five years
- Analyzing the principle of DNS resolution in kubernetes cluster
- [leetcode weekly replay] 303rd weekly 20220724
- Docker container Django + MySQL service
- Are you still using system. Currenttimemillis()? Take a look at stopwatch
- 剖析kubernetes集群内部DNS解析原理
- BGP machine room and BGP
- Promtool Check
- ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
- Measurement and Multisim Simulation of volt ampere characteristics of circuit components (engineering documents attached)
猜你喜欢
随机推荐
The new version of SSM video tutorial in shangsilicon valley was released
Server intranet and Extranet
痛并快乐的-NIO编程
Improve static loading dynamic loading
Grafana - influxdb visual K6 output
dpkg : Breaks: libapt-pkg5.0 (< 1.7~b) but 1.6.15 is to be installedE: Broken packages
Install and configure php5-7 version under centos7.4
The use of Multimeter in circuit analysis experiment of Shandong University
Weekly summary (*66): next five years
NVIDIA inspector detailed instructions
Install K6 test tool
Restructuredtext grammar summary for beginners
Flash send email
Qt项目-安防监控系统(各个界面功能实现)
[mindspore] [training warning] warning when executing training code
Soft test --- fundamentals of programming language (Part 2)
C language: deep analysis function stack frame
Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库
Mobile terminal touch event
2022 Henan Mengxin League game (2): Henan University of technology d-pair
![[acwing weekly rematch] 61st weekly 20220723](/img/8b/df2c8d516db1e7e5f2d50bcf62b2b1.png)



![[LeetCode周赛复盘] 第 303 场周赛20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)




