We sometimes encounter this when we use Selenium for web automation testingnosuchdriverexception
This exception is usually caused by the webdriver not being able to find the specified browser driver. In this post, we will discussnosuchdriverexception
Reasons and how to fix this.
nosuchdriverexception
is an anomalous class in selenium, which represents:webdriver
The specified driver could not be found. This is usually caused by the driver path setting incorrectly, the driver version not matching the browser version, or the driver not initializing correctly.
The driver path is set incorrectly
When we start the browser, Selenium needs to know where the browser driver is. If the path is set incorrectly, it will resultnosuchdriverexception
。Make sure our driver path is correct.
from selenium import webdriver Set the Chrome driver path driver path ="path/to/chromedriver"driver = webdriver.chrome(executable_path=driver_path)The driver version does not match the browser version
Because the Chrome browser is automatically updated, it is often the case that our browser driver version lags behind the browser version, so we need to check if our driver version matches the browser.
The driver is not initialized correctly
In some cases, you may need to set some additional parameters or options to initialize the driver correctly. For example, Chrome may require settings--no-sandbox
Options.
from selenium import webdriveroptions = webdriver.chromeoptions()options.add_argument('--no-sandbox')driver = webdriver.chrome(options=options)The selenium library version is too high
The latest version of Selenium may have some unstable or unknown problems, we try to avoid installing the latest versions of the library, and recommend installing a more stable version.
Update your browser drivers
If our driver doesn't match the new browser version, we can update our driver and change the chromedriver** address to the new one.
Check the path settings
Check that the driver path is set correctly. Use absolute or relative paths, making sure that the slashes or backslashes in the path are correct.
Add the necessary options and parameters
When initializing the webdriver, add the necessary options and parameters as needed. For example, you may need to add some options to bypass some security settings when using the Chrome browser.
Use WebDriverManager
usewebdrivermanager
Driver management can be simplified by automatically setting up a driver that matches your browser version. Installationwebdrivermanager
and use it to initializewebdriver
。** Below:
from selenium import webdriverfrom webdriver_manager.chrome import chromedrivermanagerdriver = webdriver.chrome(chromedrivermanager().install())Example:
from selenium import webdriverfrom webdriver_manager.chrome import chromedrivermanagertry: Initialize chrome webdriver driver driver = webdriver. with webdrivermanagerchrome(chromedrivermanager().install()) to open a web page. finally: Shut down webdriver if driver: driverquit()Downgrade the selenium version
If none of the above works, we need to downgrade the selenium version to fix the issue, it is recommended to install 46.0, the command is as follows:
pip install selenium==4.6.0This article focuses on how to solve the problem
nosuchdriverexception
question the method to ensure that the selenium test runs smoothly. Remember to choose the appropriate workaround for your situation, and keep your browser, driver updates, and installation more stableselenium
library to avoid problems.