traceback: Message: 'chromedriver' executable needs to be in PATH

Question:

I am working on this bot for a web scraping app.

import os
import booking.constanst as const
from selenium import webdriver 


class Booking(webdriver.Chrome):
    
    def __init__(self, driver_path=(r"C:UsersNarsildevseleniumDriverschromedriver.exe")):
        self.driver_path = driver_path
        os.environ['PATH'] += self.driver_path
        super(Booking, self).__init__()

    def land_page(self):
        self.get(const.BASE_URL)   

I get this error message:

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

I have tried to add the path to my enviromental variables, set the path of chromedrive and executed, but I still get this error.

Asked By: Narsil91

||

Answers:

        os.environ['PATH'] += self.driver_path

This is not the right way to set the environment variable. Actually webdriver allows you to specific the path of chromedriver binary, so you don’t need to make use of environment variable.

CHROMEDRIVER_PATH = r"C:UsersNarsildevseleniumDriverschromedriver.exe")

class Booking(webdriver.Chrome):
    
    def __init__(self, driver_path=CHROMEDRIVER_PATH):
        self.driver_path = driver_path
        super(Booking, self).__init__(executable_path=driver_path)

Besides, IMO composition is more suitable than inheritance to build page object in most cases. Here is the sample.

class BasePageObject:
  def __init__(self, driver_path):
    self.driver = webdriver.Chrome(executable_path=driver_path)

class Booking(BasePageObject):
      def land_page(self):
        self.driver.get(const.BASE_URL)   
Answered By: link89