I want to use selenium with chromedriver on Mac,but I have some troubles on it.
- I download the chromedriver from ChromeDriver - WebDriver for Chrome
- But I don't want to put it to PATH.So I do this.
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
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:
brew cask install chromedriver
which chromedriver
get the driver path- And then use it like this
webdriver.Chrome("/usr/local/bin/chromedriver")
But I don't know why so complex to use selenium.