webdriver-manager
is a Python library that can automatically download and manage Webdriver drivers, including ChromeDriver, GeckoDriver, EdgeDriver, IEDriver, etc.
The main purpose of this library is to help developers run Selenium test scripts on different operating systems and browsers, avoiding the trouble of manually downloading and installing drivers. By using webdriver-manager
, developers can:
- Automatic download and update of WebDriver driver binaries;
- Manage versions of WebDriver drivers;
- Easily switch test environments between multiple browsers and operating systems.
Using webdriver-manager
helps make your test scripts run more reliably and stably, because it ensures that the driver version matches the browser version. In addition, this library can help reduce the setup and maintenance costs of the test environment, and improve the repeatability and portability of test scripts.
Here's an webdriver-manager
example using :
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
In this example, we use to webdriver-manager
automatically download and install the latest version of ChromeDriver [compatible with the local browser] , and use it to start the Chrome browser.
If you need to use a specific version of WebDriver, you can do so by specifying the version number. For example, a specific version of ChromeDriver can be installed with the following code:
from webdriver_manager.chrome import ChromeDriverManager
# 安装 ChromeDriver 94.0.4606.41
driver_path = ChromeDriverManager(version='94.0.4606.41').install()
In this example, we use version
the parameter to specify the version number of ChromeDriver to install. This parameter can be a specific version number, or a range of versions, eg94.*
.
webdriver-manager
Ensure that the downloaded WebDriver driver is compatible with the local browser version by checking the version of the local browser. Specifically, it reads the version information of the local browser, then matches the available WebDriver driver version, selects the closest version, and downloads it locally.
If the local browser version is older, webdriver-manager
it will try to select the closest compatible version.
We can also conveniently specify the local download and save path of chromedriver, which can support both relative path and absolute path. The code example is as follows:
from webdriver_manager.chrome import ChromeDriverManager
ChromeDriverManager(path = r".\\Drivers").install()
When using webdriver-manager, how to specify the save path of the driver?
In this example, we use path
the parameter to specify the save path of ChromeDriver /path/to/driver/folder
, and webdriver-manager
ChromeDriver will be downloaded and saved under this path. If the folder does not exist, it will be created automatically .
from webdriver_manager.chrome import ChromeDriverManager
driver_path = ChromeDriverManager(path='/path/to/driver/folder').install()
How to specify the url of the image download source when using webdriver-manager?
from webdriver_manager.chrome import ChromeDriverManager
url="https://registry.npmmirror.com/-/binary/chromedriver"
latest_release_url="https://registry.npmmirror.com/-/binary/chromedriver/LATEST_RELEASE"
driver_path = ChromeDriverManager(url=url, latest_release_url=latest_release_url).install()
==================
Firefox usage
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("https://www.baidu.com/")
Edge usage
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager
driver = webdriver.Edge(EdgeChromiumDriverManager().install())
driver.get("https://www.baidu.com/")
Opera usage
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager
driver = webdriver.Opera(executable_path=OperaDriverManager().install())
driver.get("https://www.baidu.com/")
=========================
didn't knowwebdriver-manager之前,我用过的自动更新脚本如下:
# -*- coding: utf-8 -*-
import os
import requests
import winreg
import zipfile
url = 'http://npm.taobao.org/mirrors/chromedriver/'
# chromedriver download link
def get_Chrome_version():
'''查询系统内的Chrome【浏览器的版本】'''
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Google\Chrome\BLBeacon')
version, types = winreg.QueryValueEx(key, 'version')
return version
def get_version(file_path):
'''查询系统内的Chromedriver【驱动的版本】'''
outstd2 = os.popen(file_path+'chromedriver --version').read()
return outstd2.split(' ')[1]
def get_server_chrome_versions(): # 获服务器上当前的Chromedriver的版本(返回是列表)
'''return all versions list'''
versionList = []
url = "https://registry.npmmirror.com/-/binary/chromedriver/"
rep = requests.get(url).json()
for item in rep:
versionList.append(item["name"])
return versionList
def download_driver(download_url):
'''下载文件'''
file = requests.get(download_url)
with open("chromedriver.zip", 'wb') as zip_file: # 保存文件到脚本所在目录
zip_file.write(file.content)
print('下载成功')
def unzip_driver(path):
'''解压Chromedriver压缩包到指定目录'''
f = zipfile.ZipFile("chromedriver.zip", 'r')
for file in f.namelist():
f.extract(file, path)
def check_update_chromedriver(file_path): # 如果有版本号完全一致的,就直接下载对应版本的驱动,否则只匹配主版本号就可以了。
chromeVersion = get_Chrome_version()
chrome_main_version = int(chromeVersion.split(".")[0]) # chrome主版本号
driver_main_version=''
if os.path.exists(os.path.join(file_path,"chromedriver.exe")):
driverVersion = get_version(file_path)
driver_main_version = int(driverVersion.split(".")[0]) # chromedriver主版本号
download_url = ""
if driver_main_version != chrome_main_version:
print("chromedriver版本与chrome浏览器不兼容,更新中>>>")
versionList = get_server_chrome_versions()
if chromeVersion in versionList: # 如果有版本号完全一致的,就直接下载对应版本的驱动,否则只匹配主版本号就可以了。
download_url = f"{url}{chromeVersion}/chromedriver_win32.zip"
else:
for version in versionList:
if version.startswith(str(chrome_main_version)): # 检查版本号开头的字符,如果与主版本号相同,就返回对应的驱动URL
download_url = f"{url}{version}/chromedriver_win32.zip"
break
if download_url == "":
print("暂无法找到与chrome兼容的chromedriver版本,请在http://npm.taobao.org/mirrors/chromedriver/ 核实。")
download_driver(download_url=download_url)
path = file_path
unzip_driver(path)
os.remove("chromedriver.zip")
print('更新后的Chromedriver版本为:', get_version(file_path))
else:
print("chromedriver版本与chrome浏览器相兼容,无需更新chromedriver版本!")
return os.path.join(file_path,"chromedriver.exe")
if __name__ == "__main__":
file_path="C:\webdrivers\\" # 我电脑的环境变量path设置的驱动目录是"C:\webdrivers\"
print(check_update_chromedriver(file_path))