Quniao full-stack UI automation teaching (9): Why is Selenium slow? That's because you loaded the policy settings incorrectly

I. Introduction

Many fans, and even some friends with certain experience in the industry, say that selenium has poor performance and is very slow, and it takes a long time to open a web page before it starts to execute. I can understand the question raised by beginner fans, but I am a little surprised to say that with several years of experience in the industry. It seems that many small partners still do not have the habit of reading official documents. Here is a brief explanation of why you think selenium is slow and how to solve this problem:

When we start the browser through webdriver (such as starting Google Chrome: webdriver.Chrome()), it will start a browser without any cache, cookies. At this time driver.get("https://xxx"), it is naturally slow to access the page, because it needs to load the resources of the page. If its pictures, styles, and js files are too large, it will become slower at this time.

It is the same as we usually clear the cache of the browser and then visit it. This is not a flaw in the performance of Selnium itself.

But at this time, friends will definitely have doubts. Even if the browser cache is cleared and revisited, it is not as slow as running through Selnium! The button you want to click has appeared for a long time, and it still hasn't clicked. Isn't this called slow?

This is actually related to Selenium's page loading strategy.



2. Detailed explanation of Selenium's page loading strategy

Selnium has three page load strategies (pageLoadStrategy):

  1. normal: wait for the entire page to load before starting the operation
  2. eager: Wait for the entire dom tree to be loaded, that is, DOMContentLoadedthe event is completed, that is, as long as the HTML is completely loaded and parsed, the operation will start. Give up waiting for images, styles, subframes to load.
  3. none: Wait for the html download to complete, even if the parsing has not started before executing the operation.

By default, when Selenium WebDriver loads a page, it follows the normalloading strategy, so it will cause the page to load too slowly, especially when the files such as images and styles are too large, the slowness is especially obvious.


Therefore, we can adjust Selenium's page loading strategy according to the actual situation to shorten the waiting time and improve the execution speed.

The following picture is the default case (without manually specifying the loading strategy) to visit the iQIYI homepage, and then click on the movie, the total time: about 7s
insert image description here


The following picture is to use the eagerloading strategy to access the iQiyi homepage, and then click on the movie, the total time: about 3.6s

The configuration code is as follows:

chrome_options = Options()
chrome_options.page_load_strategy = 'eager'
driver = webdriver.Chrome(options=chrome_options)

insert image description here


It can be clearly seen that the access speed has become faster.


Do you think that's it? And even faster!

The loading strategy is set to none, and retryretry is introduced (the purpose is to prevent errors, of course, implicit wait can be set, but it is not safe to retry), which can be executed in about 2s . The complete code is as follows:

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)

3. Summary

So selenium isn't slow, it's just that you didn't choose right to load the strategy and mistakenly thought it was slow.

When we encounter a more puzzling or confusing problem, we should go to the official documentation for the first time, maybe it already has a solution.

In addition, this column tutorial will stop updating indefinitely, because recently I am writing a more interesting and dry column "Building an Automated Test Platform from 0". Welcome to subscribe to support!

Online demo address: http://121.43.43.59/ (Account: admin Password: 123456)


Guess you like

Origin blog.csdn.net/momoda118/article/details/120624777