在Mac上将硒与chromedriver一起使用

威克斯

我想在Mac上将硒与chromedriver一起使用,但遇到了一些麻烦。

  1. 我从ChromeDriver下载chromedriver-WebDriver for Chrome
  2. 但我不想将其放入PATH。

在此处输入图片说明

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

但这也不行。

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

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

selenium.common.exceptions.WebDriverException:消息:“ chromedriver”可执行文件必须位于PATH中。

因此,如何解决该问题,我想将其与驱动程序路径(而不是PATH)一起使用,以便可以轻松地部署我的项目。

解:

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

但是我不知道为什么使用硒如此复杂。

索拉布·高尔

selenium.common.exceptions.WebDriverException:消息:“ chromedriver”可执行文件必须位于PATH中。

要使用启动Chrome浏览器,ChromeDriver您需要将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变量设置为可执行文件

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将chromedriver与硒ubuntu一起使用

在Mac OS上将OpenMP与C ++ 11一起使用

是否可以将Google Chrome浏览器代替chromedriver与硒一起使用?

在Mac OSX上将JDK 7或更高版本与Android Studio和Eclipse一起使用

在Intel Mac OS X上将PATH AMPL求解器与Pyomo一起使用

Chromedriver 在路径中工作但不与硒一起工作(python)

如何在Kubernetes上将WebRTC与RTCPeerConnection一起使用?

在Windows 7上将phantomjs与RSelenium一起使用

在Windows上将Apache Tomcat与IntelliJ一起使用

如何在 Ubuntu 上将 tsify 与 watchify 一起使用?

在Windows上将结构与Golang syscall一起使用?

在Windows 10的Ubuntu上将Cmder与Bash一起使用

在AWS Lambda上将Django与sqlite一起使用

如何在cyginw上将prezto与zsh一起使用

在macOS上将终端键绑定与bash一起使用

在顶层地图上将@JacksonInject与@JsonCreator一起使用

在Windows上将git与ssh-agent一起使用

在Windows上将Ngrok与IISExpress一起使用

在Android上将ListView或TextView与ArrayAdapter一起使用?

在iOS上将Halide提前(AOT)与Metal一起使用

在Borland C ++上将#pragma pack与#define一起使用

在Ubuntu上将Movemail与Thunderbird一起使用

在Emacs上将Hlint与Intero一起使用

在Windows上将OpenCV与Clion IDE一起使用

在Moq上将接口与UserManager一起使用

无法在 Windows 上将 OSG 与 GDAL 一起使用

在one.com上将smtp与php一起使用

在Windows 7上将通配符与mysqlimport一起使用

在Mac OS上将frei0r滤镜与ffmpeg一起使用时获取黑色图片