Friday, April 26, 2019

How to Fix “Unable to delete chromedriver.exe” Error in Selenium



How to Fix “Unable to delete chromedriver.exe” Error in Selenium

When running Selenium tests, you may occasionally encounter an error like this:

Unable to delete file "C:\...\bin\Debug\chromedriver.exe".
Access to the path 'C:\...\bin\Debug\chromedriver.exe' is denied.

At first glance, this looks like a simple permission issue. However, in most cases, the real cause is that the chromedriver process is still running in the background.

This usually happens when:

  • The test did not shut down properly

  • The WebDriver instance was not fully disposed

  • A previous run left orphaned driver processes

In my case, the file remained locked after test execution, causing repeated build failures.

Quick Fix

The fastest way to resolve this is to manually terminate the running driver processes.

Open Command Prompt and run:

taskkill /f /im chromedriver.exe
taskkill /f /im geckodriver.exe

Example output:

SUCCESS: The process "chromedriver.exe" with PID 17196 has been terminated.
SUCCESS: The process "geckodriver.exe" with PID 8844 has been terminated.

After killing these processes, you should be able to rebuild and rerun your tests without issues.

Preventing the Issue

To avoid this problem in the future, make sure your test framework properly cleans up driver instances:

  • Always call driver.Quit()

  • Use [TearDown] or [OneTimeTearDown] in NUnit

  • Ensure cleanup runs even on failure (use try-finally)

Final Thoughts

This is a small but frustrating issue that can interrupt your workflow.
By ensuring proper driver cleanup and knowing how to quickly kill leftover processes, you can keep your Selenium tests running smoothly.

If you want, I can also tailor this for Playwright or add C# code examples for cleanup.