当前位置:网站首页>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
边栏推荐
- 【加密周报】加密市场有所回温?寒冬仍未解冻!盘点上周加密市场发生的重大事件!
- Alibaba cloud free SSL certificate application detailed process
- 一个函数中写多少行代码比较合适呢? 代码整洁之道
- Actual combat of MySQL database design project of online mall system
- Imperial cms7.5 imitation "question and answer library" question and answer learning platform website source code with mobile version
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(1)开发概要
- Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!
- Single arm routing experiment demonstration (Huawei router device configuration)
- 哈希无向图可视化
- 华为交换机系统软件升级和安全漏洞修复教程
猜你喜欢

MES管理系统有什么应用价值

华为交换机系统软件升级和安全漏洞修复教程

阿里云免费SSL证书申请详细流程

SQL Server 2019 installation tutorial

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

Youth, oh, youth

Common development software download addresses

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

Imeta | sangerbox: interactive integrated clinical information analysis platform

【DETR用于3D目标检测】3DETR: An End-to-End Transformer Model for 3D Object Detection
随机推荐
Gan, why ".Length! == 3??
Improvement of wechat applet 26 playing music page ②
微信小程序 26 播放音乐页的完善②
Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
InTouch advanced alarm (alarm filtering)
小程序毕设作品之微信校园维修报修小程序毕业设计成品(1)开发概要
HTTP cache tongtianpian, there may be something you want
Pyqt5 click qtableview vertical header to get row data and click cell to get row data
前夕 - 0day威胁情报
How to be a self disciplined person?
【iniparser】项目配置工具iniparser的简单使用
帝国CMS7.5仿《问答库》题库问答学习平台网站源码 带手机版
Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list
Pymoo学习 (8):Gradients
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
QIIME2得到PICRUSt2结果后如何分析
【小程序开发】常用组件及基本使用详解
The bank's wealth management subsidiary accumulates power to distribute a shares; The rectification of cash management financial products was accelerated
Swift 基础 Codable(JSONEncoder JSONDecoder)的使用
Huawei switch system software upgrade and security vulnerability repair tutorial