在 chrome 中运行 Selenium WebDriver python 绑定

新手上路,请多包涵

我在使用 Selenium 时遇到了问题。对于我的项目,我必须使用 Chrome。但是,使用 Selenium 启动该浏览器后,我无法连接到该浏览器。

出于某种原因,Selenium 无法自行找到 Chrome。这就是我尝试在不包含路径的情况下启动 Chrome 时发生的情况:

 Traceback (most recent call last):
  File "./obp_pb_get_csv.py", line 73, in <module>
    browser = webdriver.Chrome() # Get local session of chrome
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
    self.service.start()
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 58, in start
    and read up at http://code.google.com/p/selenium/wiki/ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.                 Please download from http://code.google.com/p/selenium/downloads/list                and read up at http://code.google.com/p/selenium/wiki/ChromeDriver'

为了解决这个问题,我在启动 Chrome 的代码中包含了 Chromium 路径。但是,解释器找不到要连接的套接字:

 Traceback (most recent call last):
  File "./obp_pb_get_csv.py", line 73, in <module>
    browser = webdriver.Chrome('/usr/bin/chromium') # Get local session of chrome
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
    self.service.start()
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 64, in start
    raise WebDriverException("Can not connect to the ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the ChromeDriver'

我还尝试通过启动 chrome 来解决问题:

铬--远程外壳端口=9222

但是,这也不起作用。

PS。以下是有关我的系统的一些信息:

 www-客户端:铬 15.0.874.121
开发语言:python 2.7.2-r3 Selenium 2.11.1
操作系统:GNU/Linux Gentoo 内核 3.1.0-gentoo-r1

原文由 Akendo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 774
1 个回答

您需要确保独立的 ChromeDriver 二进制文件(不同于 Chrome 浏览器二进制文件)在您的路径中或在 webdriver.chrome.driver 环境变量中可用。

有关如何连接的完整信息,请参阅 http://code.google.com/p/selenium/wiki/ChromeDriver

编辑:

对,这似乎是 Python 绑定中的一个错误,它从路径 环境变量中读取 chromedriver 二进制文件。似乎 chromedriver 不在您的路径中,您必须将其作为参数传递给构造函数。

 import os
from selenium import webdriver

chromedriver = "/Users/adam/Downloads/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://stackoverflow.com")
driver.quit()

原文由 adam goucher 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题