How to fix Selenium Error (Python) - Message: 'chromedriver.exe' executable needs to be in PATH

Viewed 70

from selenium import webdriver from selenium.webdriver.chrome.service import Service

class Pinterest:
   def __init__(self, email: str, password: str) -> None:
    
    self.email = email  
    self.password = password 
    self.webdriver_path = os.path.abspath('assets/chromedriver.exe')
    self.driver = self.webdriver()  
    self.login_url = 'https://www.pinterest.com/login/'
    self.upload_url = 'https://www.pinterest.com/pin-builder/'

   def webdriver(self):
    
    options = webdriver.ChromeOptions()  
    options.add_argument('--lang=en')  
    options.add_argument('log-level=3')  
    options.add_argument('--mute-audio')  
    driver = webdriver.Chrome(service=Service(  
        self.webdriver_path), options=options) 
    driver.maximize_window()  
    return driver
1 Answers

This is because when you create the chrome web driver using

driver = webdriver.Chrome(service=Service(self.webdriver_path),options=options) 

the absolute path of the chromewebdriver.exe will be taken from PATH environment variable. In order to resolve this issue, you could either pass the path explicitly to the webdriver.Chrome class. ie,

driver = webdriver.Chrome(executable_path="<FULL PATH OF .EXE>", service=Service(self.webdriver_path),options=options) 

Or

Add the web driver path to the PATH environment variable

Related