在Chrome中运行Selenium WebDriver python绑定 [英] Running Selenium WebDriver python bindings in chrome

查看:192
本文介绍了在Chrome中运行Selenium WebDriver python绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  Traceback(最近一次调用最后一次):
文件./obp_pb_get_csv.py,第73行,位于< module>
browser = webdriver.Chrome()#获取本地会话的ch
文件/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py,第46行,在__init__
self.service.start()
文件/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py,第58行,在
并在http://code.google.com/p/selenium/wiki/ChromeDriver上阅读)
selenium.common.exceptions.WebDriverException:消息:'ChromeDriver可执行文件需要在路径中可用。请从http://code.google.com/p/selenium/downloads/list下载并阅读http://code.google.com/p/selenium/wiki/ChromeDriver'

为了解决这个问题,我在启动Chrome的代码中加入了Chromium路径,但是解释器无法找到连接的套接字到:

  Traceback(最近一次调用最后一次):
文件./obp_pb_get_csv.py,第73行,在< module> $ b $中b browser = webdriver.Chrome('/ usr / bin / chromium')#获取chrome的本地会话
文件/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py ,第46行,在__init__
self.service.start()
文件/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py,第64行,在开始
提高WebDriverException(无法连接到ChromeDriver)
selenium.common.exceptions.WebDriverException:消息:'无法连接到ChromeDriver'


铬--remote-shell-port = 9222 



然而,这也不起作用。



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

 www-client:chromium 15.0.874.121 
dev-lang:python 2.7.2-r3 Selenium 2.11。 1
操作系统:GNU / Linux Gentoo Kernel 3.1.0-gentoo-r1


解决方案

请确保独立的ChromeDriver二进制文件(与Chrome浏览器二进制文件不同)位于您的路径中,或者位于webdriver.chrome.driver环境变量中。



请参阅< href =http://code.google.com/p/selenium/wiki/ChromeDriver> http://code.google.com/p/selenium/wiki/ChromeDriver ,了解有关线缆



编辑:

好的,似乎是Python阅读chromedriver的Python绑定中的一个错误二进制来自路径环境变量。似乎chromedriver不在你的路径中,你必须将它作为参数传递给构造函数。

  import os 
from selenium import webdriver

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


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.

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'

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'

I also tried solving the problem by launching chrome with:

chromium --remote-shell-port=9222

However, this did not work either.

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

解决方案

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.

see http://code.google.com/p/selenium/wiki/ChromeDriver for full information on how wire things up.

Edit:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆