在Mac上使用含有chromedriver的硒

时间:2016-09-10 16:12:58

标签: python selenium

我想在Mac上使用chlenium和chromedriver,但我有一些麻烦。

  1. 我从ChromeDriver - WebDriver for Chrome
  2. 下载了chromedriver
  3. 但我不想把它放到PATH。所以我这样做。
  4. 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/')
    

    但我无法得到我想要的结果。

    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. 然后我运行brew cask install chromedriver。我只运行它 没有司机路径。

      browser = webdriver.Chrome()
      browser.get('http://www.baidu.com/')
      
    2. 但它也无法发挥作用。

      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
      

      最后我尝试将它放到/ usr / bin

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

      我尝试在.zshrc中使用export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac。但是

        

      selenium.common.exceptions.WebDriverException:消息:&#39; chromedriver&#39;可执行文件需要在PATH中。

      所以如何解决它,我想将它与不在PATH中的驱动程序路径一起使用,这样我就可以轻松部署我的项目了。

      解决方案:

      1. brew cask install chromedriver
      2. which chromedriver获取驱动程序路径
      3. 然后像webdriver.Chrome("/usr/local/bin/chromedriver")
      4. 一样使用它
          

        但我不知道为什么使用硒这么复杂。

4 个答案:

答案 0 :(得分:4)

  

selenium.common.exceptions.WebDriverException:消息:&#39; chromedriver&#39;可执行文件需要在PATH中。

To launch chrome browser using ChromeDriver您需要将executable chromedriver位置与可执行文件本身一起传递到executable_path

你应该尝试如下: -

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

或将PATH变量using command with executable设置为: -

export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac

然后尝试将ChromeDriver初始化为: -

from selenium import webdriver

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

答案 1 :(得分:2)

对我来说,这样工作不会使事情复杂化

  1. 从官方 link (Chrome浏览器的公告版本)下载 chromedriver
  2. 解压缩 *。zip 文件和文件 chromedriver 并复制到位置 usr / local / bin /
  3. 删除您放入文件中的所有路径,然后使用 driver = webdriver.Chrome()
  4. 如果探针仍然存在,请尝试重新打开PyCharm,因为有时需要重新打开PyCharm

答案 2 :(得分:1)

为简单起见:

从此link下载chrome webdriver。 复制&#39; chromedriver&#39;在python脚本的文件夹中。

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

答案 3 :(得分:0)

对我来说,通过自制软件安装chromedriver就像在MacOS 11上一样具有魅力。

brew install chromedriver && brew update