我在使用 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 来解决问题:
chromium --remote-shell-port=9222
但是,这也不起作用。
PS。以下是关于我的系统的一些信息:
www-client: chromium 15.0.874.121 dev-lang: python 2.7.2-r3 Selenium 2.11.1 OS: GNU/Linux Gentoo Kernel 3.1.0-gentoo-r1
最佳答案
您需要确保独立的 ChromeDriver 二进制文件(不同于 Chrome 浏览器二进制文件)在您的路径中或在 webdriver.chrome.driver 环境变量中可用。
见 http://code.google.com/p/selenium/wiki/ChromeDriver有关如何连接的完整信息。
编辑:
对,这似乎是 Python 绑定(bind)中的一个错误,即从路径 或 环境变量读取 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()
关于python - 在 chrome 中运行 Selenium WebDriver python 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255929/