Use selenium with chromedriver on Mac
<p>I want to use selenium with chromedriver on Mac,but I have some troubles on it.</p>

<ol>
<li>I download the chromedriver from <a href="https://sites.google.com/a/chromium.org/chromedriver/home" rel="nofollow">ChromeDriver - WebDriver for Chrome</a></li>
<li>But I don't want to put it to PATH.So I do this.</li>
</ol>

<p><a href="http://i.stack.imgur.com/iz0kG.png" rel="nofollow"><img src="http://i.stack.imgur.com/iz0kG.png" alt="enter image description here"></a></p>

<pre><code>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/')
</code></pre>

<p>But I can't get the result I want.</p>

<pre><code>Traceback (most recent call last):
  File "/Users/wyx/project/python-scraping/se/test.py", line 15, in &lt;module&gt;
    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 &lt;bound method Service.__del__ of &lt;selenium.webdriver.chrome.service.Service object at 0x107f96150&gt;&gt; ignored
</code></pre>

<ol start="3">
<li><p>Then I run <code>brew install chromedriver</code>.And I only run this 
without the driver path.</p>

<pre><code>browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
</code></pre></li>
</ol>

<p>But it can't work either.</p>

<pre><code>Traceback (most recent call last):
  File "/Users/wyx/project/python-scraping/se/test.py", line 16, in &lt;module&gt;
    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 &lt;bound method Service.__del__ of &lt;selenium.webdriver.chrome.service.Service object at 0x105c08150&gt;&gt; ignored

Process finished with exit code 1
</code></pre>

<p>Finally I try to put it to /usr/bin</p>

<pre><code>â  Downloads sudo cp chromedriver /usr/bin
Password:
cp: /usr/bin/chromedriver: Operation not permitted
</code></pre>

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

<blockquote>
  <p>selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.</p>
</blockquote>

<p>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.</p>

<p>Solution:</p>

<ol>
<li><code>brew install chromedriver</code></li>
<li><code>which chromedriver</code> get the driver path</li>
<li>And then use it like this <code>webdriver.Chrome("/usr/local/bin/chromedriver")</code></li>
</ol>

<blockquote>
  <p>But I don't know why so complex to use selenium.</p>
</blockquote>

<blockquote>
  <p>selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.</p>
</blockquote>

<p><a href="http://stackoverflow.com/questions/8255929/running-webdriver-chrome-with-selenium">To launch chrome browser using <code>ChromeDriver</code></a> you need to pass <a href="https://sites.google.com/a/chromium.org/chromedriver/home" rel="nofollow">executable chromedriver</a> location with executable itself into <code>executable_path</code>.</p>

<p>You should try as below :-</p>

<pre><code>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/')
</code></pre>

<p>Or set <code>PATH</code> variable <a href="http://codrspace.com/rails4sandeep/install-and-set-path-to-chromedriver-on-mac-or-linux/" rel="nofollow">using command with executable</a> as :-</p>

<pre><code>export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac
</code></pre>

<p>Then try to Initialize <code>ChromeDriver</code> as :-</p>

<pre><code>from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
</code></pre>

python selenium