在Chrome中运行Selenium WebDriver Python绑定

154 投票
9 回答
262010 浏览
提问于 2025-04-17 06:59

我在使用Selenium的时候遇到了一个问题。为了我的项目,我需要用Chrome浏览器。但是,在用Selenium启动Chrome后,我无法连接到这个浏览器。

出于某种原因,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

不过,这也没有成功。

顺便说一下,这是我系统的一些信息:

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

9 个回答

94

仅适用于Mac OS X

如果你已经安装了Homebrew(如果还没有,建议你先去安装,这样会让你的生活更轻松),那么有个更简单的方法可以开始使用。你只需要运行以下命令:

brew install chromedriver

这样就会把chromedriver放到你的路径里,你就可以开始使用了。

115

对于Linux系统

  1. 首先,检查一下你是否安装了最新版本的Chrome浏览器,输入命令:chromium-browser -version

  2. 如果没有安装最新版本,可以用这个命令来安装:sudo apt-get install chromium-browser

  3. 接下来,从这里下载合适版本的Chrome驱动程序。

  4. 解压下载的chromedriver.zip文件。

  5. 把解压出来的文件移动到/usr/bin目录,使用命令:sudo mv chromedriver /usr/bin

  6. 然后,进入/usr/bin目录,输入命令:cd /usr/bin

  7. 现在,你需要运行这个命令:sudo chmod a+x chromedriver,这样才能让它可以执行。

  8. 最后,你就可以执行你的代码了。

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("http://www.google.com")
    print driver.page_source.encode('utf-8')
    driver.quit()
    
137

你需要确保独立的 ChromeDriver 程序(它和 Chrome 浏览器的程序是不同的)要么在你的系统路径中,要么在 webdriver.chrome.driver 这个环境变量中可用。

想了解更多信息,可以查看这个链接:http://code.google.com/p/selenium/wiki/ChromeDriver

编辑:

对了,似乎在使用 Python 的时候,读取 chromedriver 程序时出现了个 bug。如果 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()

撰写回答