Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use selenium with chromedriver on Mac

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.

like image 250
wyx Avatar asked Sep 10 '16 16:09

wyx


People also ask

How do I run Chromedriver on Mac?

Unable to launch the chrome browser“, you need to go to usr/local/bin folder and right-click chromeDriver file and open it. After this step, re-run your tests, chrome driver will work. Also, you can use Bonigarcia Webdriver Manager library in your project, for this you need to add its dependency in your project.

Can you use Selenium on Mac?

Follow the below steps to install the Selenium package on macOS using the setup.py file: Step 1: Download the latest source package of Selenium for python3 from here. Step 2: Extract the downloaded package using the following command. Step 3: Go inside the folder and Enter the following command to install the package.

What is the extension of Chromedriver in Mac?

You can install both packed (. crx file) and unpacked (directory) extensions via ChromeDriver.


1 Answers

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
like image 61
Milos Avatar answered Sep 22 '22 05:09

Milos