首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Mac上使用selenium和chromedriver

在Mac上使用selenium和chromedriver
EN

Stack Overflow用户
提问于 2016-09-11 00:12:58
回答 4查看 38.4K关注 0票数 11

我想在Mac上使用selenium和chromedriver,但我遇到了一些麻烦。

我从ChromeDriver - WebDriver for Chrome

  • But下载了chromedriver,我不想把它放到,我这样做。

代码语言:javascript
复制
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/')

但是我不能得到我想要的结果。

代码语言:javascript
复制
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.And,我只在没有驱动路径的情况下运行。

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

但它也不能工作。

代码语言:javascript
复制
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中

代码语言:javascript
复制
➜  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。但

chromedriver消息:‘

selenium.common.exceptions.WebDriverException:’可执行文件需要在PATH中。

所以如何解决它,我想使用它的驱动路径,而不是在路径中,这样我就可以轻松地部署我的项目。

解决方案:

  1. brew cask install chromedriver
  2. which chromedriver获取
  3. 的驱动程序路径,然后像下面这样使用它

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-09-11 00:48:35

chromedriver消息:‘

selenium.common.exceptions.WebDriverException:’可执行文件需要在PATH中。

To launch chrome browser using ChromeDriver你需要将executable chromedriver位置连同可执行文件本身一起传递到executable_path中。

你应该尝试如下所示:

代码语言:javascript
复制
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/')

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

代码语言:javascript
复制
export PATH=$PATH:/Users/wyx/project/python-scraping/se/bin/chromedriver_for_mac

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

代码语言:javascript
复制
from selenium import webdriver

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
票数 13
EN

Stack Overflow用户

发布于 2018-06-05 09:11:39

为了简单起见:

从这个link下载chrome webdriver。复制python脚本文件夹中的'chromedriver‘。

代码语言:javascript
复制
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()
票数 7
EN

Stack Overflow用户

发布于 2019-08-17 05:58:43

因为我是这样工作的,不会让事情变得复杂

  1. 从官方网站下载chromedriver (注意Chrome browser)
  2. Unpack *.zip文件和文件的版本chromedriver拷贝到您放入文件中的任何路径并使用<代码>E115driver= webdriver.Chrome()
  3. If probem exist尝试重新打开PyCharm,因为有时需要重新打开才能正常工作
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39428042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档