Home > Net >  traceback: Message: 'chromedriver' executable needs to be in PATH
traceback: Message: 'chromedriver' executable needs to be in PATH

Time:08-30

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:\Users\Narsil\dev\seleniumDrivers\chromedriver.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.

CodePudding user response:

        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:\Users\Narsil\dev\seleniumDrivers\chromedriver.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.get(const.BASE_URL)   
  • Related