当前位置:网站首页>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
边栏推荐
- Actual combat of MySQL database design project of online mall system
- The finished product of wechat campus maintenance and repair applet graduation design (1) development outline
- HTTP缓存通天篇,可能有你想要的
- Telnet installation and telnet (correct password) cannot log in!
- 基于FPGA的1080P 60Hz BT1120接口调试过程记录
- i3-status 配置
- 【阅读笔记】《深度学习》第一章:引言
- Pymoo学习 (8):Gradients
- 乐理基础 调式
- Youfu network was invited to attend the 2022 national CIO conference and won the title of "CIO trusted brand"
猜你喜欢

21 days proficient in typescript-4 - type inference and semantic check

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

KCon 2022 亮点及议程大揭秘!

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

微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建

前夕 - 0day威胁情报

Pymoo learning (6): termination conditions

Intouch高级报警(报警筛选)

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

Youth, oh, youth
随机推荐
小程序毕设作品之微信校园维修报修小程序毕业设计成品(2)小程序功能
2022 IAA industry category development insight series report - phase II
i3-status 配置
Youfu network was invited to attend the 2022 national CIO conference and won the title of "CIO trusted brand"
Hongmeng - Damiao computing Sketchpad - VIDEO
PHP等于==和恒等于===的区别
房地产行业大洗牌
In the first half of the year, the shipment volume has exceeded that of the whole year of last year, and centritec millimeter wave radar has "captured" the international giant
Actual combat of MySQL database design project of online mall system
歌曲转调之后和弦如何转换
PyQt5单击QTableView垂直表头verticalHeader获取行数据以及单击单元格获取行数据操作
I3 status configuration
帝国CMS7.5仿《问答库》题库问答学习平台网站源码 带手机版
Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function
有孚网络受邀参加2022全国CIO大会并荣获“CIO信赖品牌”称号
SQL Server 2019 安装教程
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 change the chords after the tune of the song is changed
Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
telnet安装以及telnet(密码正确)无法登录!