当前位置:网站首页>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
边栏推荐
- [iniparser] simple use of the project configuration tool iniparser
- Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal
- Amrita Institute of Engineering | reinforcement active learning method for optimizing sampling in terms extraction of emotional analysis
- Scala foundation [set 01]
- Leetcode skimming: dynamic programming 07 (different binary search trees)
- 高端旗舰投影仪选购指南:当贝X3 Pro、当贝F5观影更沉浸!
- Why learn service grid istio
- 房地产行业大洗牌
- Binary tree visualization
- 【DETR用于3D目标检测】DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D Queries
猜你喜欢

Introduction to web security ICMP testing and defense

微信小程序10-微搭模板

Talk about 15 tips of SQL optimization

Kcon 2022 highlights and agenda revealed!
![[hdlbits questions] Verilog language (3) modules: hierarchy section](/img/35/ccdbb55aa0aff7e9dec2bf9e64c4e2.png)
[hdlbits questions] Verilog language (3) modules: hierarchy section

The second "future Cup" knowledge map championship was officially launched

leetcode刷题:动态规划07(不同的二叉搜索树)

Flutter tips: optimizing the buildcontext you use
![Scala foundation [set 01]](/img/6b/0f5da7ea923ef3aa436d7af9c4425c.png)
Scala foundation [set 01]

Is there a "fingerprint" in the structure of AAAI 2022 | Gan? Generating network structure from forged image traceability
随机推荐
鸿蒙-大喵计算画板-简介
哪吒 D1-H 测试 microbench
[record of question brushing] 21. Merge two ordered linked lists
University of California | feasible confrontation robust reinforcement learning for unspecified environments
Selenium运行慢 - 通过设置selenium加载策略加快运行速度
leetcode刷题:动态规划07(不同的二叉搜索树)
i3-status 配置
IP地址的概念
平衡二叉树
GBASE 8s UDR内存管理_01_mi_alloc
Why learn service grid istio
How many lines of code is appropriate for a function? Clean Code
Small program completion work wechat campus maintenance application small program graduation design finished product (2) small program function
Youfu force supercomputing provides customized high-performance computing services for customers
哈希无向图可视化
[919. Complete binary tree inserter]
微信小程序 29 热搜榜的完善②
Solve the problem that the win10 account has no administrator rights
NPM semantic version control, solution console prop being mutated: "placement" error
Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list