首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在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

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
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39428042

复制
相关文章

相似问题

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