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.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
wyx
  • 3,334
  • 6
  • 24
  • 44

4 Answers4

14

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/')
Greg Sadetsky
  • 4,863
  • 1
  • 38
  • 48
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • This two methods I have tried.It show me the same error what I have said above @Saurabh Gaur – wyx Sep 11 '16 at 03:03
  • I know you've tried but you haven't pass executable chromedriver itself with location that's why you're in trouble – Saurabh Gaur Sep 11 '16 at 03:06
  • To lunch ChromeDriver you need to pass executable directory with executable file also not only directory, make attention – Saurabh Gaur Sep 11 '16 at 03:08
  • @wyx And you have to pass executable chromedriver path into `executable_path` variable, see provided answer carefully.. – Saurabh Gaur Sep 11 '16 at 03:11
  • chromedriver_for_mac is not a directory ,ok?My screenshots shows it clearly. – wyx Sep 11 '16 at 03:13
  • Oh, that's my mistake but you have to pass this into `executable_path` as `webdriver.Chrome(executable_path = DRIVER_BIN)` – Saurabh Gaur Sep 11 '16 at 03:17
  • Once move this executable to some other fresh empty dir location from current and then try – Saurabh Gaur Sep 11 '16 at 03:21
  • `webdriver.Chrome("/usr/local/bin/chromedriver")` only this can work. The driver is installed by brew. But why. – wyx Sep 11 '16 at 03:40
  • 1
    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`..:) – Saurabh Gaur Sep 11 '16 at 04:08
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()
Hassan Rahman
  • 4,953
  • 1
  • 34
  • 32
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
Milos
  • 71
  • 1
  • 1
3

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

brew install chromedriver && brew update 
  • this worked for me too. Also, remember apple might block this app from starting remember to unblock it from security & privacy. – Mahesh Jamdade Dec 21 '21 at 09:17
  • 1
    as of 2023, the command is `brew install chromedriver --cask` and location is `/opt/homebrew/bin/chromedriver` – james-see Jan 19 '23 at 16:14