当前位置:网站首页>Selenium运行慢 - 通过设置selenium加载策略加快运行速度
Selenium运行慢 - 通过设置selenium加载策略加快运行速度
2022-07-25 19:21:00 【闪亮伞】
一、前言
有不少粉丝,甚至一些行业中有一定经验的朋友都说selenium性能差、很慢,打开一个网页要等半天才开始执行。初学的粉丝提出这个问题我能够理解,行业工作经验数年还这样说我就有点诧异了。看来很多小伙伴还是没有阅读官方文档的习惯,这里就简单的讲解一下为什么你会觉得selenium慢以及如何解决这个问题:
当我们通过webdriver(比如启动谷歌浏览器:webdriver.Chrome())启动浏览器时,会启动一个没有任何缓存、cookie的浏览器。这个时候通过driver.get(“https://xxx”)去访问页面自然是慢的,因为它需要加载该页面的资源,如果它的图片、样式、js文件过大时,这个时候就会变得更慢。
跟我们平时直接将浏览器清除缓存再访问是一个道理,这并不是Selnium本身性能存在缺陷。
但这时小伙伴们肯定会有疑惑,就算清了浏览器缓存重新访问,也没有通过Selnium来跑的那么慢呀!想要点击的按钮都出现半天了,它都还不进行点击,这不叫慢吗?
这其实跟Selenium的页面加载策略有关。
Selnium的页面加载策略(pageLoadStrategy)
- normal:等待整个页面加载完毕再开始执行操作
- eager:等待整个dom树加载完成,即DOMContentLoaded这个事件完成,也就是只要 HTML完全加载和解析完毕就开始执行操作。放弃等待图片、样式、子帧的加载。
- none:等待html下载完成,哪怕还没开始解析就开始执行操作。
默认情况下,当 Selenium WebDriver
加载页面时,它遵循的是normal加载策略,所以就会导致页面加载过慢,特别是在图片、样式等文件过大时,慢的就尤其明显了。
所以我们可以根据实际情况调整Selenium的页面加载策略来缩短等待时间,提高执行速度。
加载策略设置为eager
配置代码如下:
chrome_options = Options()
chrome_options.page_load_strategy = 'eager'
driver = webdriver.Chrome(options=chrome_options)
加载策略设置为none
加载策略设置为none,并引入retry做重试(目的是为了防止报错,当然设置隐式等待也可,但没retry稳妥)可以只用2s左右就能执行完成,完整代码如下:
import datetime
from retrying import retry # 需第三方库,需pip进行安装
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
@retry(wait_fixed=10, stop_max_attempt_number=1)
def click(path):
driver.find_element(By.XPATH, path).click()
chrome_options = Options()
chrome_options.page_load_strategy = 'none'
driver = webdriver.Chrome(options=chrome_options)
start_time = datetime.datetime.now()
driver.get('https://www.iqiyi.com/')
click('//*[@id="block-C"]/div/div/div/div[1]/div[1]/div/div[1]/div/div/a/span[2]')
end_time = datetime.datetime.now()
print(end_time - start_time)
三、总结
所以selenium并不慢,只是你加载策略没有选择对而误认为它慢。
当我们遇到比较费解或疑惑的问题时,应该第一时间去看看官方文档,说不定它已经有了解决方案了。
————————————————
原文链接:https://blog.csdn.net/m0_67393619/article/details/123321768
边栏推荐
- 聊聊接口性能优化的11个小技巧
- 微信小程序 28 热搜榜的完善①
- FPGA based 1080p 60Hz bt1120 interface debugging process record
- [hdlbits questions] Verilog language (3) modules: hierarchy section
- C 调的满级和玄
- [applet development] do you know about applet development?
- 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
- The difference between QT exec and show
- Improvement of wechat applet 29 hot search list ②
- 【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
猜你喜欢

SQL Server 2019 installation tutorial

Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template

房企打响“保交战”
![[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection](/img/22/426bcb8641db5bfe07e8aacf5e8427.png)
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection

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

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

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

Alibaba cloud free SSL certificate application detailed process

Solve the problem that the win10 account has no administrator rights

Pymoo学习 (5):收敛性分析
随机推荐
果链“围城”:傍上苹果,是一场甜蜜与苦楚交错的旅途
MES管理系统有什么应用价值
Common development software download addresses
乐理基础 调式
小程序毕设作品之微信校园维修报修小程序毕业设计成品(6)开题答辩PPT
虹科分享|如何解决勒索软件安全漏洞
C 调的满级和玄
Full scale and Xuan of C key
Pymoo learning (5): convergence analysis
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection
有孚网络受邀参加2022全国CIO大会并荣获“CIO信赖品牌”称号
鸿蒙-大喵计算画板-简介
[record of question brushing] 21. Merge two ordered linked lists
Imeta | sangerbox: interactive integrated clinical information analysis platform
【iniparser】项目配置工具iniparser的简单使用
[hdlbits questions] Verilog language (3) modules: hierarchy section
Hongmeng - Damiao computing Sketchpad - VIDEO
哈希无向图可视化
Improvement of wechat applet 29 hot search list ②
【DETR用于3D目标检测】DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D Queries