Use selenium with chromedriver on Mac

For me worked like this without complicating things

  1. Download chromedriver from official link (notice version of Chrome browser)
  2. Unpack *.zip file and file chromedriver copy to location usr/local/bin/
  3. Remove any path you put in file and just go with driver = webdriver.Chrome()
  4. If probem still exist try to reopen PyCharm since sometimes need to be reopened in case to work

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

To launch chrome browser using ChromeDriver you need to pass executable chromedriver location with executable itself into executable_path.

You should try as below :-

import os
from selenium import webdriver

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "bin/chromedriver_for_mac")

browser = webdriver.Chrome(executable_path = DRIVER_BIN)
browser.get('http://www.baidu.com/')

Or set PATH variable using command with executable as :-

export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac

Then try to Initialize ChromeDriver as :-

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')

For sake of simplicity:

Download the chrome webdriver from this link. Copy the 'chromedriver' in the folder of python script.

from selenium import webdriver
import os

url = 'http://www.webscrapingfordatascience.com/complexjavascript/'

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "chromedriver")

driver = webdriver.Chrome(executable_path = DRIVER_BIN)

driver.get(url)

input('Press ENTER to close the automated browser')

driver.quit()

For me, installing chromedriver through homebrew worked like a charm on MacOS 11.

brew install chromedriver && brew update