当前位置:网站首页>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
边栏推荐
- [Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection
- Basic music theory -- configuring chords
- i3-status 配置
- Solve the problem that the win10 account has no administrator rights
- 基于FPGA的1080P 60Hz BT1120接口调试过程记录
- Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt
- The finished product of wechat campus maintenance and repair applet graduation design (1) development outline
- 有孚网络受邀参加2022全国CIO大会并荣获“CIO信赖品牌”称号
- How to analyze qiime2 after obtaining picrust2 results
- 基础乐理--配置和弦
猜你喜欢

基础乐理--配置和弦

【Web技术】1391- 页面可视化搭建工具前生今世

How to ensure the consistency of double write between database and cache?
![[iniparser] simple use of the project configuration tool iniparser](/img/2b/1d20b4ef44dfe2544891d9c72b676e.png)
[iniparser] simple use of the project configuration tool iniparser

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

What is the application value of MES management system

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

微信小程序 26 播放音乐页的完善②

聊聊sql优化的15个小技巧

Improvement of wechat applet 28 hot search list ①
随机推荐
阿里云免费SSL证书申请详细流程
[record of question brushing] 21. Merge two ordered linked lists
Pyqt5 click qtableview vertical header to get row data and click cell to get row data
MES管理系统有什么应用价值
Real estate enterprises have launched a "war of guarantee"
Improvement of wechat applet 29 hot search list ②
leetcode刷题:动态规划07(不同的二叉搜索树)
Gan, why ".Length! == 3??
600000 pieces of data are made from March 1 to March 31. Videodate requires starting time from 00:00 to 24:00 on March 1 to 31, which is only for notes
这种动态规划你见过吗——状态机动态规划之股票问题(上)
果链“围城”:傍上苹果,是一场甜蜜与苦楚交错的旅途
How to be a self disciplined person?
InTouch advanced alarm (alarm filtering)
Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt
Hongmeng - Damiao computing Sketchpad - VIDEO
Imperial cms7.5 imitation "question and answer library" question and answer learning platform website source code with mobile version
The degree of interval of basic music theory
【919. 完全二叉树插入器】
Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report
二叉树可视化