在 chrome 中运行 Selenium WebDriver python 绑定 [英] Running Selenium WebDriver python bindings in chrome
问题描述
我在使用 Selenium 时遇到了问题.对于我的项目,我必须使用 Chrome.但是,使用 Selenium 启动该浏览器后,我无法连接到该浏览器.
I ran into a problem while working with Selenium. For my project, I have to use Chrome. However, I can't connect to that browser after launching it with Selenium.
由于某种原因,Selenium 无法自己找到 Chrome.当我尝试在不包含路径的情况下启动 Chrome 时会发生这种情况:
For some reason, Selenium can't find Chrome by itself. This is what happens when I try to launch Chrome without including a path:
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 路径.但是,解释器无法找到要连接的套接字:
To solve this problem, I then included the Chromium path in the code that launches Chrome. However, the interpreter fails to find a socket to connect to:
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 来解决问题:
I also tried solving the problem by launching chrome with:
chromium --remote-shell-port=9222
然而,这也不起作用.
附注.以下是有关我的系统的一些信息:
PS. Here's some information about my system:
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 中可用环境变量.
You need to make sure the standalone ChromeDriver binary (which is different than the Chrome browser binary) is either in your path or available in the webdriver.chrome.driver environment variable.
参见 http://code.google.com/p/selenium/wiki/ChromeDriver有关如何连接的完整信息.
see http://code.google.com/p/selenium/wiki/ChromeDriver for full information on how wire things up.
是的,似乎是 Python 绑定中的一个错误,它可以从路径或环境变量中读取 chromedriver 二进制文件.似乎如果 chromedriver 不在您的路径中,您必须将其作为参数传递给构造函数.
Right, seems to be a bug in the Python bindings wrt reading the chromedriver binary from the path or the environment variable. Seems if chromedriver is not in your path you have to pass it in as an argument to the constructor.
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()
这篇关于在 chrome 中运行 Selenium WebDriver python 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!