Selenium Tutorial 05: Use webdriver-manager to automatically download the browser driver, so you no longer have to worry about the driver version.
Selenium Tutorial 05: Use webdriver-manager to automatically download the browser driver, so you no longer have to worry about the driver version.
WebDriverManager is a tool for managing Web drivers, mainly used in the field of automated testing . When conducting Selenium testing, a web driver that matches the browser is required to control and operate the browser. WebDriverManager can automatically download and manage browser drivers, automatically detect the browsers installed in the local system, and download the corresponding browser drivers (such as Chrome Driver, Firefox Driver, etc.). This avoids the tedious process of manually downloading and configuring drivers, improving the efficiency and stability of automated testing to use these browsers in automated testing.
Module installation command
pip install webdriver_manager
1. Download the Google driver to the specified new_path directory and verify whether the driver is available. If the Baidu webpage can be opened, it means the driver is available and the installation is successful. For writing methods of selenium 4.+ and above, here we mainly demonstrate the usage of selenium 4.+ new version + Google driver. For old versions or other types of browsers, you can copy the relevant code yourself.
# @Author : Little Red Bull
# WeChat : WdPython
# selenium 4.+
import shutil
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
# 1. Install ChromeDriver using ChromeDriverManager and return the path to the driver.
driver_path = ChromeDriverManager().install()
# Print the driver path
print(driver_path)
# C:\Users\Ms-xiao\.wdm\drivers\chromedriver\win64\120.0.6099.109\chromedriver-win32/chromedriver.exe
new_path = 'D:/[python](/search?q=python)/driver/'
# 2. Copy the downloaded driver file to the specified location
shutil.copy(driver_path, new_path)
# 3. Verify that the installed driver is available.
driver = webdriver.Chrome(service=ChromeService(new_path + 'chromedriver.exe'))
# Open Baidu page
driver.get('https://www.baidu.com')
How to use Selenium 4.0 and below
# Use this method for Selenium 4.0 and below.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
# Install ChromeDriver using ChromeDriverManager and return the path to the driver
driver_path = ChromeDriverManager().install()
# Print the driver path
print(driver_path)
# Create the Chrome WebDriver and specify the driver path
driver = webdriver.Chrome(executable_path=driver_path)
# Open a Baidu web page
driver.get("https://www.baidu.com")
2. How to download other browser drivers
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import IEDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.opera import OperaDriverManager
# 1. FireFox
GeckoDriverManager().install()
# 2. Microsoft Internet Explorer
IEDriverManager().install()
# 3. Microsoft Edge
EdgeChromiumDriverManager().install()
# 4. Opera
OperaDriverManager().install()
3. Regarding whether automated testing comes with path instructions.
In the following code, when webdriver.Chrome creates the driver object, it brings a specified driver path. This way of writing is not wrong. Whether to bring it or not depends on the location of chromedriver.exe.
new_path = 'D:/Wdpython/driver/'
driver = webdriver.Chrome(service=ChromeService(new_path + 'chromedriver.exe'))
If chromedriver.exe is in a certain file or a cdfe drive letter, you need to bring a specified path. If not, the driver cannot be started and an error message will appear. If you don't want to bring it, you can do it this way. You only need to put chromedriver.exe outside and in the same directory as the project folder, as shown in the figure below. Then write the code and it can be abbreviated as follows. It will detect the path by default. The specific method to use depends on your personal habits and preferences.
driver = webdriver.Chrome()
complete! ! thanks for watching
----------★★Historical blog post collection★★----------
My zero-based [Python tutorial](/search?q=Python tutorial), Python introduction to advanced video tutorial Py installation py project Python module Python crawler Json