chromedriver.exe file does not exist in the current directory

By FoxLearn 12/21/2024 3:05:59 AM   14
If you're using Selenium with ChromeDriver and encounter the following exception:
Unhandled Exception: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html

This error typically indicates that Selenium can't locate the chromedriver.exe file. The chromedriver.exe is essential for controlling Chrome browser instances when using Selenium WebDriver.

Install the Selenium.Chrome.WebDriver NuGet Package

The simplest way to resolve this issue is by installing the Selenium.Chrome.WebDriver NuGet package. This package automatically downloads the correct version of chromedriver.exe and makes it available for your project.

Open the Package Manager Console in Visual Studio by navigating to View > Other Windows > Package Manager Console, then run the following command:

Install-Package Selenium.Chrome.WebDriver

This command installs the Selenium.Chrome.WebDriver package and places the chromedriver.exe in your build directory

ChromeDriver Version Incompatibility

Another common issue arises when the version of chromedriver.exe is incompatible with the installed version of the Chrome browser.

You may see errors like:

System.InvalidOperationException: session not created: Chrome version must be between 70 and 73
(Driver info: chromedriver=2.45.615291

System.InvalidOperationException: session not created: This version of ChromeDriver only supports Chrome version 85 (SessionNotCreated)

This issue typically occurs when you're trying to use a version of ChromeDriver that doesn't match your Chrome browser version.

If you encounter a version mismatch, the Selenium.Chrome.WebDriver NuGet package may not always include the latest version of chromedriver.exe. In such cases, you can manually download the correct version of ChromeDriver that matches your installed version of Chrome.

You can find the version of chromedriver.exe: Download ChromeDriver

Once you've downloaded the correct chromedriver.exe version, add it to your project. Right-click on your project and select Add > Existing Item, then choose the chromedriver.exe file.

Mark the chromedriver.exe as Copy to Output Directory and set it to Copy if newer. This ensures that chromedriver.exe is copied to your build directory each time you build your project.

Update your Selenium code to use the newly added chromedriver.exe. You can specify the location of chromedriver.exe by passing the chromeDriverDirectory parameter.

var options = new ChromeOptions();
options.BinaryLocation = @"C:\Path\To\Chrome\chrome.exe"; // Optional: specify the Chrome binary location
IWebDriver driver = new ChromeDriver(chromeDriverDirectory: @"C:\Path\To\Chromedriver", options);
driver.Navigate().GoToUrl("https://www.google.com");

You can handle multiple versions of Chrome and ChromeDriver simultaneously. For example, if you have several versions of Chrome installed, you can download and specify the corresponding ChromeDriver for each version.

If you're encountering the DriverServiceNotFoundException or version incompatibility errors with Selenium and ChromeDriver, the solutions above should help you resolve them.