当前位置:网站首页>Selenium 设置元素等待的三种方式详解
Selenium 设置元素等待的三种方式详解
2022-07-25 19:21:00 【闪亮伞】
Selenium 设置元素等待的三种方式
1. sleep 强制等待
2. implicitly_wait() 隐性等待
3. WebDriverWait() 显示等待
三种方式的优缺点
sleep 强制等待
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
sleep(2) #设置等待2秒钟
driver.get('http://www.baidu.com')
- 优点: 代码简介,简单明了
- 缺点:如果设置sleep等待时间过短,元素还没加载出来,程序报错,sleep设置等待时间过长,元素早就加载出来了,程序还在等待,浪费是时间,影响代码整体的运行效率
- 个人看法: 简单粗暴,根据网站的响应速度和自己的网速来设置合理的休眠时间
implicitly_wait() 隐性等待
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.implicitly_wait(20) #设置等待20秒钟
driver.get('http://www.baidu.com')
- 优点:
1.代码简介
2.在代码前部分加implicitly_wait(10) ,整个的程序运行过程中都会有效(作用于全局,直接在初始化driver的后面加,后面的代码都会受影响),都会等待元素加载完成
3.在设置的时间内没有加载到整个页面,则会报NosuchElementError。如果元素在第10s被加载出来,自动执行下面的脚本,不会一直等待10s
- 缺点:
- 非要加载到整个页面才执行代码,这样影响代码的执行效率,一般情况下,我们想要的结果是只需加载到了我要定位的元素就执行代码,不需要等待整个页面的完全加载出来再执行代码。
- 个人看法:
1.不适合用在数据在ajax的网站中,比如翻页什么的,某个元素一直存在,但是数据一直在变,这样的话只要加载出来第一页,后面翻页的数据全部会和第一页的数据相同,因为代码判断了这个元素已经被加载出来了,不会等ajax去加载
WebDriverWait()显示等待
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait #WebDriverWait注意大小写
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('元素定位报错%s'%message)
finally:
pass
- 优点:
代码执行效率快。无需等待整个页面加载完成,只需加载到你要定位的元素就可以执行代码。是最智能的设置元素等待的方式。
- 缺点:
1.要导入
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
必须要导入以上3个包,导包路径相当的复杂,啰嗦而且麻烦
2.写等待时间的代码也是复杂。步骤稍微有点多。
element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,‘kw’)))
element.send_keys(‘123’)
- 个人看法:
相比于两种,这种方式可以算的上好的了,但是就是麻烦,写的代码太多,使用的话可以和第一种方式sleep混合使用,不过我还是喜欢用sleep,本身使用selenium就是没办法破开网站,或者使用selenium比直接破解的方式更好才使用这种,我个人是能不用就不用,抓取速度太慢了。
————————————————
原文链接:https://blog.csdn.net/Gscsd_T/article/details/102837046
边栏推荐
- JS 基本类型 引用类型 深/浅克隆复制
- Intouch高级报警(报警筛选)
- Monitor MySQL based on MySQL exporter
- 某公司网络设计与规划
- Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list
- 【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
- 这种动态规划你见过吗——状态机动态规划之股票问题(上)
- Pymoo learning (7): Parallelization
- i3-status 配置
- Basic music theory -- configuring chords
猜你喜欢

Modelsim and quartus jointly simulate PLL FIFO and other IP cores

【阅读笔记】《深度学习》第一章:引言

iMeta | Sangerbox: 交互式整合临床生信分析平台

Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report

Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list

Basic music theory -- configuring chords

Pymoo learning (5): convergence analysis

小程序毕设作品之微信校园维修报修小程序毕业设计成品(2)小程序功能

Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function

华为交换机系统软件升级和安全漏洞修复教程
随机推荐
房企打响“保交战”
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
Flutter 小技巧之优化你使用的 BuildContext
Pymoo学习 (6):终止条件
SQL Server 2019 installation tutorial
【小程序开发】宿主环境详解
聊聊sql优化的15个小技巧
【919. 完全二叉树插入器】
The difference between QT exec and show
英诚医院内部网络规划与设计
Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function
Full scale and Xuan of C key
How to ensure the consistency of double write between database and cache?
JS 基本类型 引用类型 深/浅克隆复制
SQL Server 2019 安装教程
Pymoo learning (5): convergence analysis
Real estate industry reshuffle
How many lines of code is appropriate for a function? Clean Code
歌曲转调之后和弦如何转换
The degree of interval of basic music theory