I want to use selenium with chromedriver on Mac,but I have some troubles on it.
For me worked like this without complicating things
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()
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 :-
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 me, installing chromedriver through homebrew worked like a charm on MacOS 11.
brew install chromedriver && brew update