当前位置:网站首页>Selenium runs slowly - speed up by setting selenium load policy
Selenium runs slowly - speed up by setting selenium load policy
2022-07-25 19:29:00 【Shining umbrella】
One 、 Preface
There are many fans , Even some friends with some experience in the industry say selenium Poor performance 、 Very slowly , It takes half a day to open a web page . Novice fans ask this question, I can understand , I'm a little surprised to say that after years of working experience in the industry . It seems that many little friends still don't have the habit of reading official documents , Here's a brief explanation of why you feel selenium Slow and how to solve this problem :
When we go through webdriver( For example, start Google browser :webdriver.Chrome()) When you start the browser , Will start one without any cache 、cookie Browser . This time through driver.get(“https://xxx”) It's naturally slow to visit the page , Because it needs to load the resources of the page , If its picture 、 style 、js When the document is too large , This time it will become slower .
It's the same reason that we usually clear the browser cache and then access it , This is not Selnium Its performance is defective .
But at this time, the little friends must have doubts , Even if the browser cache is cleared, access again , It didn't pass Selnium Come and run so slowly ! All the buttons you want to click have appeared for half a day , It doesn't even click , Isn't that slow ?
This is actually the same as Selenium Page loading policy .
Selnium Page loading strategy (pageLoadStrategy)
- normal: Wait for the entire page to load before starting the operation
- eager: Waiting for the whole dom Tree loading complete , namely DOMContentLoaded This event is complete , As long as HTML After full loading and parsing, the operation begins . Give up waiting for pictures 、 style 、 Subframe loading .
- none: wait for html Download complete , Even if the operation starts before parsing .
By default , When Selenium WebDriver
When loading the page , It follows normal Load policy , So it will cause the page to load too slowly , Especially in pictures 、 When files such as styles are too large , The slow is especially obvious .
So we can adjust according to the actual situation Selenium Page loading strategy to shorten the waiting time , Improve execution speed .
The loading policy is set to eager
The configuration code is as follows :
chrome_options = Options()
chrome_options.page_load_strategy = 'eager'
driver = webdriver.Chrome(options=chrome_options)
The loading policy is set to none
The loading policy is set to none, And introduce retry Try again ( The purpose is to prevent error reporting , Of course, setting implicit waiting can also , But no retry Err on the side of ) It can be used only 2s It can be executed around , The complete code is as follows :
import datetime
from retrying import retry # Third party library required , Need to be pip Installation
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)
3、 ... and 、 summary
therefore selenium Not slow , It's just that you don't choose the right loading strategy and mistakenly think it's slow .
When we encounter problems that are difficult to understand or confused , You should look at the official documents as soon as possible , Maybe it already has a solution .
————————————————
Link to the original text :https://blog.csdn.net/m0_67393619/article/details/123321768
边栏推荐
- [hdlbits questions] Verilog language (3) modules: hierarchy section
- The second "future Cup" knowledge map championship was officially launched
- Code sharing of social chat platform developed by dating website (III)
- 基于海思3559 高效率的 0延时 0拷贝 qt播放器方案
- SDL text display
- Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function
- 【DETR用于3D目标检测】3DETR: An End-to-End Transformer Model for 3D Object Detection
- Introduction of this course (Introduction to machine learning)
- binarySearch基础二分查找
- Swift 基础 Codable(JSONEncoder JSONDecoder)的使用
猜你喜欢

Leetcode skimming: dynamic programming 07 (different binary search trees)

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

Use of swift basic codable (jsonencoder jsondecoder)
![[reading notes] deep learning Chapter 1: Introduction](/img/b3/58f71b032cd8f04ecf95d48281a41a.png)
[reading notes] deep learning Chapter 1: Introduction

某公司网络设计与规划

ML的编程技巧:

微信小程序开发之WXSS模板样式与WXS脚本语言

NPM semantic version control, solution console prop being mutated: "placement" error

InTouch advanced alarm (alarm filtering)

“未来杯”第二届知识图谱锦标赛正式启动
随机推荐
Pymoo learning (6): termination conditions
Flutter tips: optimizing the buildcontext you use
手机端触摸图片slider轮播插件photoswipe.js
JS learning notes 17: DOM query exercise
微信小程序10-微搭模板
Fearless of high temperature and rainstorm, how can Youfu network protect you from worry?
[server data recovery] a data recovery case of a brand ProLiant server raid paralysis, database file loss, and database file backup damage
SDL text display
安全基础6 ---漏洞复现
小程序毕设作品之微信校园维修报修小程序毕业设计成品(6)开题答辩PPT
一个函数中写多少行代码比较合适呢? 代码整洁之道
IP地址的概念
高端旗舰投影仪选购指南:当贝X3 Pro、当贝F5观影更沉浸!
Kcon 2022 highlights and agenda revealed!
给容器添加3d效果的副标题
GBASE 8s UDR内存管理_03_mi_realloc
Illegal mix of collations for operation ‘UNION‘(bug记录)
iMeta | Sangerbox: 交互式整合临床生信分析平台
Youfu network was invited to attend the 2022 national CIO conference and won the title of "CIO trusted brand"
Imeta | sangerbox: interactive integrated clinical information analysis platform