Automated management of chromedriver-perfect solution to version mismatch problem

Python Selenium Automation - Automatically manage chromedriver

  • Previously we introduced how to use Python Selenium for browser automation testing, and the detailed examples provided have already provided a good introduction to how to use ChromeDriverManager to automatically manage ChromeDriver.
    How to use ChromeDriverManager to manage ChromeDriver
  • Recently, chrome has automatically updated to version 116, but chromedriver is still stuck at version 114. The above code will automatically download the latest version 114 of chromedriver in the current directory. It can only be used normally when the two are updated simultaneously.

Solution: Lock down older versions of Chrome

Chrome automatically updates quickly and newer older versions of Chrome are difficult to find. An easy way to do this is to lock Chrome to an older version, such as 104.
You can find the installation package here
Google Chrome Older Versions Download (Windows, Linux &Mac).
The download and installation will be downloaded to this directory C:\Program Files (x86)\Google
lock method. Please refer to this article to
completely turn off automatic updates of the Chrome browser.

Then we use the chromedriver_autoinstaller module, which can automatically check the current Chrome browser version and download the latest matching chromedriver.
Steps for usage:

  1. Install chromedriver_autoinstaller
   pip install chromedriver_autoinstaller
  1. Import the module and call the install method to install the matching chromedriver
 import chromedriver_autoinstaller
 chromedriver_autoinstaller.install()
  1. Create ChromeDriver instance
driver = webdriver.Chrome()

Complete code and examples

This version of chromedriver will be downloaded to this directory in the C drive

C:\Users\你的用户名\.cache\selenium\chromedriver\win32\104.0.5112.79
import time

import chromedriver_autoinstaller
from selenium.webdriver.common.by import By
from selenium import webdriver

# 自动安装匹配版本的 Chromedriver,首次下载时间较长,
# 首次下载后使用时注释掉,否则会重复下载
chromedriver_autoinstaller.install()

# 创建一个 Chrome 浏览器实例
driver = webdriver.Chrome()

# 使用浏览器访问网页
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys("selenium")
driver.find_element(By.ID, "su").click()
time.sleep(2)
driver.quit()

The above code needs to be commented to avoid repeated downloads when used for the second time, which is not perfect.
Optimized code

import time
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import os
import chromedriver_autoinstaller

# 获取当前文件所在目录的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 设置驱动下载目录为当前目录下的drivers文件夹
driver_path = os.path.join(current_dir, 'drivers')
os.makedirs(driver_path, exist_ok=True)

# 提示用户选择是否下载新的 Chromedriver
user_choice = input("是否更新 Chromedriver?(y/n): ")
if user_choice.lower() == 'y':
    # 自动安装最新版本的 Chromedriver
    chromedriver_autoinstaller.install(path=driver_path)

    # 创建Service对象,传入chromedriver路径
    chromedriver_path = os.path.join(driver_path, chromedriver_autoinstaller.get_chrome_version())
    service = Service(chromedriver_path)

    # 创建一个 Chrome 浏览器实例,传入驱动服务对象
    driver = webdriver.Chrome(service=service)
else:
    # 如果你不使用104版本的chrome,需要改成你之前下载好的路径,跳过下载
    service = Service("drivers/104/chromedriver.exe")
    driver = webdriver.Chrome(service=service)

# 使用浏览器访问网页
driver.get("https://www.baidu.com")
driver.find_element(By.ID, "kw").send_keys("selenium")
driver.find_element(By.ID, "su").click()
time.sleep(2)

# 关闭浏览器
driver.quit()

Now the chromedriver version will always match the Chrome browser version, avoiding version incompatibility issues.

Summarize

chromedriver_autoinstaller can automatically manage the chromedriver version and is an essential tool for Python Selenium testers. It can improve code portability and reduce failures caused by version issues. It is recommended to use them in combination to make automated testing more stable and reliable.

Practice: Practical combat of crawler data cleaning and visualization - analysis of employment situation

Guess you like

Origin blog.csdn.net/qq_42531954/article/details/132613060