15

I want to use selenium with chromedriver on Mac,but I have some troubles on it.

  1. I download the chromedriver from ChromeDriver - WebDriver for Chrome
  2. But I don't want to put it to PATH.So I do this.

enter image description here

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")
print DRIVER_BIN
browser = webdriver.Chrome(DRIVER_BIN)
browser.get('http://www.baidu.com/')

But I can't get the result I want.

Traceback (most recent call last):
  File "/Users/wyx/project/python-scraping/se/test.py", line 15, in <module>
    browser = webdriver.Chrome(DRIVER_BIN)
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver_for_mac' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x107f96150>> ignored
  1. Then I run brew cask install chromedriver.And I only run this without the driver path.

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

But it can't work either.

Traceback (most recent call last):
  File "/Users/wyx/project/python-scraping/se/test.py", line 16, in <module>
    browser = webdriver.Chrome()
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.chrome.service.Service object at 0x105c08150>> ignored

Process finished with exit code 1

Finally I try to put it to /usr/bin

➜  Downloads sudo cp chromedriver /usr/bin
Password:
cp: /usr/bin/chromedriver: Operation not permitted

and I try to use export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac in .zshrc . But

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

So how to solve it,I want to use it with the driver path not in the PATH so I can deploy my project easily.

Solution:

  1. brew cask install chromedriver
  2. which chromedriver get the driver path
  3. And then use it like this webdriver.Chrome("/usr/local/bin/chromedriver")

But I don't know why so complex to use selenium.

1
  • as of 2023, the brew install chromedriver --cask location is /opt/homebrew/bin/chromedriver Commented Jan 19, 2023 at 16:14

4 Answers 4

15

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/')
Sign up to request clarification or add additional context in comments.

9 Comments

This two methods I have tried.It show me the same error what I have said above @Saurabh Gaur
I know you've tried but you haven't pass executable chromedriver itself with location that's why you're in trouble
To lunch ChromeDriver you need to pass executable directory with executable file also not only directory, make attention
@wyx And you have to pass executable chromedriver path into executable_path variable, see provided answer carefully..
Actually you are modifying the name of chromedriver to chromedriver_for_mac that's why @wyx it couldn't find when you installed through brew, and this case you just Initialize as webdriver.Chrome() and it itself would find the chromedriver executable from bin folder but it should be named as chromedriver..:)
|
8

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()

Comments

7

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

1 Comment

One step to mention at the end: if not done, allow chromedriver in the security & privacy menu on your mac
3

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

brew install chromedriver && brew update 

2 Comments

this worked for me too. Also, remember apple might block this app from starting remember to unblock it from security & privacy.
as of 2023, the command is brew install chromedriver --cask and location is /opt/homebrew/bin/chromedriver

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.