Selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH error with Headless Chrome In Python

Selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH error with Headless Chrome In Python is a popular error which happens when we are not able to find the chromedriver executable binary.

In this article we will try to replicate and solve this error while trying to understand why the error happened in the first place.

Describing : Selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH error with Headless Chrome In Python

We are trying to create a Selenium automation script

To replicate the error copy the lines bellow to your empty script

                                                                       #
import os  
from selenium import webdriver  
from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.common.keys import Keys  
                                                                       #
                                                                       #
chrome_options = Options()  
chrome_options.add_argument("--headless")  
chrome_options.binary_location = 
r'C:\Users\ishaq\Desktop\chrome\chromedriver.exe'    
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"),   
chrome_options=chrome_options)  
driver.get("http://www.twitter.com") 
                                                                       #

Do not forget to put all the snippets of code inside the same script file

                                                                         #
  menu_button = driver.find_element_by_css_selector(".menu-trigger.local")  
  menu_button.click() 
                                                                       #
                                                                       #
search_field.clear()  
search_field.send_keys("Olabode")  
                                                                       #

After running the Selenium script above we get the error bellow

                                                                       #
Traceback (most recent call last):
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
                                                                       #

Solution : use webdriver-manager since Python cannot find the binary file of the chromedriver

The source of the error is that Python cannot find the binary file of the chromedriver, this is one of the most common errors in Python.

First we need to install the webdriver-manager, we can do that using the command bellow

                                                                       #
pip install webdriver-manager
                                                                       #

Now that we have the webdriver manager installed the rest is going to be easy, let us run the line

                                                                       #
from selenium import webdriver  
                                                                       #

Followed by the line

                                                                       #
from selenium.webdriver.chrome.options import Options 
                                                                       #

You can now continue writing your code like you did before, just do not remove those two lines from the top of your script.

The error should be solved for you, cheers.

Summing-up

This is the end of our article, I hope the solution we offered solved your issue, If you like our effort and explanations and want to support our team, please consider donating to our Kofi account above, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/