0

I have been trying to work on my first web-scraping project for which I am using Selenium. However, I seem to be running into some issues with importing the ChromeDriver. I am using Selenium 3.0.0 and am working on Chrome.

webdriver_service = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")

# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')

I keep getting the following message: 'chromedriver.exe' executable needs to be in PATH.

Let me know if there's some issue with the file path I am using as I think that is where the issue is coming from.

4 Answers4

0

As answer tells, you should specify the chromedriver-path as following:

driver = webdriver.Chrome('/Users/MyUsername/Downloads/chromedriver.exe')

The executable_path= argument stands for chrome.exe, meaning google-chrome browser.

kaliiiiiiiii
  • 925
  • 1
  • 2
  • 21
0

This should be done through the OS when possible and not through code as it needs to be done for every project this way, but it is possible to set environment variables through code.

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

# Setting said environment variable
os.environ["CHROME_DRIVER_PATH"] = "Z:\\port\\driver\\chromedriver"

# Reading the variable we created
DRIVER_PATH = os.environ.get('CHROME_DRIVER_PATH')
s=Service(DRIVER_PATH)

I got my answer from here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dot
  • 64
  • 7
0

I'm going to be honest and say the path looks good to me. I even tried to run it in Visual Studio and it worked perfectly, so I have no idea why it isn't working for you. That being said I do have a way you can fix it. You can just not use executable_path. You already imported webdriver whose sole purpose is to handle the webdriver for selenium for you.

See here.

You don't even have to change much.

#pip install webdriver-manager, pip install selenium==3.0.0

import os
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")

# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')


driver.get('https://www.google.com/')

Hope this helps.

0

You have to take care of a couple of things:

  • As you are using Selenium 3.0.0 you don't have to create any Service() object. So you can remove the line:

    webdriver_service = Service(ChromeDriverManager().install())
    
  • Incase you don't intend to use ChromeDriverManager() you can remove the following lines:

    # Silent download of drivers
    logging.getLogger('WDM').setLevel(logging.NOTSET)
    os.environ['WDM_LOG'] = 'False'
    
  • Unless you are executing your test in headless mode you won't require the argument --headless. So you can remove the line:

    chrome_options.add_argument("--headless") # Ensure GUI is off
    
  • Unless you are executing your test as a root/administrator you won't nee the argument --no-sandbox. So you can remove the line:

    chrome_options.add_argument("--no-sandbox")
    

Now you can download the matching ChromeDriver version from ChromeDriver page, unzip/untar it and use the following lines of code:

from selenium import webdriver

driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # for windows OS use chromedriver.exe
driver.get('https://www.google.com/')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352