Solve – WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH while setting UserAgent through Selenium Chromedriver Python

WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH while setting UserAgent through Selenium Chromedriver Python is an error that occurs when the ChromeDriver is not found within the locations specified within the PATH variable.

Describing WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH while setting UserAgent through Selenium Chromedriver Python

The error occurs when for example you try to change the user agent, here is an example of this particular case presented in the code bellow.

                                                                       #
from selenium import webdriver
chrome_path = r'C:\Users\Desktop\chromedriver_win32\chromedriver.exe'   
driver = webdriver.Chrome(chrome_path)
options = webdriver.ChromeOptions()
options.add_argument('user-agent = Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
driver = webdriver.Chrome(chrome_options=options)
                                                                       #

When the code bellow is executed we get the following error.

                                                                       #
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
                                                                       #

In the solution bellow we will try to explain why this error is occurring and how I replicated it and solved it, we will also some other possible solutions.

Solution 1 : put ChromeDriver inside the location specified within the PATH variable

The error above means that the ChromeDriver is not present inside the location specified within the PATH variable that exists inside the Environment Variables.

In order to solve the issue we need to do a lot of things at the same time, please do not miss anything.

Format your code like the code bellow, in this code we are passing the Value that refers to the absolute path of the ChromeDriverplus the ChromeOptions object as the argument.

We are also passing the Key executable_path and also initializing both the WebBrowser and the WebDriver.

                                                                       #
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('user-agent = Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Users\Desktop\chromedriver_win32\chromedriver.exe')
driver.get('https://www.google.com')
                                                                       #

The solution above worked for me and should be enough to solve the issue.

Solution 2 : install webdriver-manager

The second and only solution left is to use the webdriver-manager, please start by running the code bellow

                                                                       #
pip install webdriver-manager
                                                                       #

Now change the code in the absolute top of the article with the following code, the only change we have made is using the webdriver manager.

                                                                       #
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
                                                                       #

The solution above is supposed to be universal and work for all browsers and work for windows and Linux.

If you find the article useful please consider donating to our Kofi account in order to help the team, you can use the red button above.

Summing-up

This is the end of our article, I hope one of the above solutions was enough to solve the issue, make sure to keep learning and keep coding, Python is awesome and it is a skill worth learning, cheers.

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