当前位置:网站首页>Detailed explanation of three methods of selenium setting element waiting
Detailed explanation of three methods of selenium setting element waiting
2022-07-25 19:29:00 【Shining umbrella】
Selenium There are three ways to set element waiting
1. sleep Mandatory waiting
2. implicitly_wait() The recessive waiting
3. WebDriverWait() According to wait
The advantages and disadvantages of the three methods
sleep Mandatory waiting
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
sleep(2) # Set the wait 2 Second
driver.get('http://www.baidu.com')
- advantage : Code Introduction , Simple and clear
- shortcoming : If you set sleep The waiting time is too short , The element hasn't been loaded yet , Application error ,sleep Set the waiting time too long , The element has already been loaded , The program is still waiting , Waste is time , Affect the overall efficiency of the code
- Personal view : Simple and crude , Set a reasonable sleep time according to the response speed of the website and your own network speed
implicitly_wait() The recessive waiting
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.implicitly_wait(20) # Set the wait 20 Second
driver.get('http://www.baidu.com')
- advantage :
1. Code Introduction
2. Add implicitly_wait(10) , The whole process will be effective ( Act on the whole , I'm just going to initialize it driver Add... After , The following code will be affected ), Will wait for the element to be loaded
3. The whole page is not loaded within the set time , Will report NosuchElementError. If the element is in the 10s Be loaded out , Automatically execute the following script , Not always waiting for 10s
- shortcoming :
- You have to load the entire page to execute the code , This affects the efficiency of code execution , In general , The result we want is to execute the code just by loading the element I want to locate , There is no need to wait for the entire page to be fully loaded before executing the code .
- Personal view :
1. Not suitable for data in ajax Web site , Like turning the page or something , An element always exists , But the data keeps changing , In this case, just load the first page , The data on the next page will be the same as the data on the first page , Because the code judges that this element has been loaded , Won't wait ajax To load the
WebDriverWait() According to wait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait #WebDriverWait Pay attention to case
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
try:
element =
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))
element.send_keys('123')
driver.find_element_by_id('su').click()
except Exception as message:
print(' Element positioning error %s'%message)
finally:
pass
- advantage :
Fast code execution . Don't wait for the entire page to load , Just load the element you want to locate and execute the code . It is the most intelligent way to set element waiting .
- shortcoming :
1. To import
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
You must import the above 3 A package , The guided package path is quite complex , Wordy and troublesome
2. Writing waiting time code is also complex . There are a little more steps .
element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,‘kw’)))
element.send_keys(‘123’)
- Personal view :
Compared to two , This way can be considered as a good , But it's just trouble , Write too much code , It can be used in the first way sleep A mixture of , But I still like to use sleep, Self use selenium Just can't break the website , Or use selenium It is better than direct cracking to use this , Personally, I don't need it if I can , Grabbing speed is too slow .
————————————————
Link to the original text :https://blog.csdn.net/Gscsd_T/article/details/102837046
边栏推荐
- Code sharing of social chat platform developed by dating website (III)
- 阿姆利塔工程学院|用于情感分析的方面术语提取中优化采样的强化主动学习方法
- Research and application of servo driver in robot
- 【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
- [server data recovery] a data recovery case of a brand ProLiant server raid paralysis, database file loss, and database file backup damage
- [record of question brushing] 21. Merge two ordered linked lists
- Nezha d1-h test microbench
- University of California | feasible confrontation robust reinforcement learning for unspecified environments
- Scala基础【集合01】
- How to ensure the consistency of double write between database and cache?
猜你喜欢

Actual combat of MySQL database design project of online mall system

授权无线通信标准

Why learn service grid istio

Hongmeng - Damiao computing Sketchpad - Introduction

手机端触摸图片slider轮播插件photoswipe.js

Wxss template style and WXS scripting language for wechat applet development

【DETR用于3D目标检测】3DETR: An End-to-End Transformer Model for 3D Object Detection

AAAI 2022 | GAN的结构有“指纹”吗?从伪造图像溯源生成网络结构

哪吒 D1-H 测试 microbench

ML的编程技巧:
随机推荐
What is the application value of MES management system
Introduction of this course (Introduction to machine learning)
小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能
Imeta | sangerbox: interactive integrated clinical information analysis platform
微信小程序开发之全局配置与页面配置
[hdlbits questions] Verilog language (3) modules: hierarchy section
[Detr for 3D object detection] detr3d: 3D object detection from multi view images via 3D-to-2D queries
加州大学|用于未指定环境的可行对抗鲁棒强化学习
NPM semantic version control, solution console prop being mutated: "placement" error
SDL text display
Code sharing of social chat platform developed by dating website (III)
Beihang and other "deep learning event extraction" literature review paper, 27 page PDF describes the current trend
Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal
Kcon 2022 highlights and agenda revealed!
微信小程序开发之WXSS模板样式与WXS脚本语言
Hongmeng - Damiao computing Sketchpad - Introduction
485 current acquisition module dam-8041
Telnet installation and telnet (correct password) cannot log in!
Network packet multi-layer transmission demonstration
Security foundation 6 - vulnerability recurrence