首页 文章

在chrome中运行Selenium WebDriver python绑定

提问于
浏览
141

我在使用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

7 回答

  • 23

    您需要确保独立的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()
    
  • 102

    For Linux

    • 检查您是否安装了最新版本的chrome brwoser-> chromium-browser -version

    • 如果没有,请安装最新版本的chrome sudo apt-get install chromium-browser

    • here获取相应版本的chrome驱动程序

    • 解压缩chromedriver.zip

    • 将文件移至 /usr/bin 目录 sudo mv chromedriver /usr/bin

    • 转到 /usr/bin 目录 cd /usr/bin

    • 现在,您需要运行类似 sudo chmod a+x chromedriver 的内容来标记它是可执行文件 .

    • 最后你可以执行代码 .

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

    Mac OSX only

    一个更简单的方法(假设你已经安装了homebrew,你应该,如果没有,那么首先去做,让自制程序让你的生活变得更好)就是运行以下命令:

    brew install chromedriver
    

    那应该把chromedriver放在你的道路上,你应该全力以赴 .

  • 92

    对于Windows

    this direct link OR 下载ChromeDriver从this page获取最新版本 .

    chromedriver.exe 文件粘贴到 C:\Python27\Scripts 文件夹中 .

    这应该现在工作:

    from selenium import webdriver
    driver = webdriver.Chrome()
    
  • 51

    对于Windows,请将 chromedriver.exe 放在 <Install Dir>/Python27/Scripts/

  • 1

    有两种方法可以在Google Chrome中运行Selenium python测试 . 我正在考虑Windows(在我的情况下是Windows 10):

    Prerequisite: 从以下位置下载最新的Chrome驱动程序:https://sites.google.com/a/chromium.org/chromedriver/downloads

    Way 1:

    i)将下载的zip文件解压缩到您选择的目录/位置
    ii)在代码中设置可执行路径如下:

    self.driver = webdriver.Chrome(executable_path='D:\Selenium_RiponAlWasim\Drivers\chromedriver_win32\chromedriver.exe')
    

    Way 2:

    i)只需将chromedriver.exe粘贴到/ Python / Scripts /下(在我的情况下,文件夹是:C:\ Python36 \ Scripts)
    ii)现在编写如下简单代码:

    self.driver = webdriver.Chrome()
    
  • 126

    对于Windows的IDE:

    如果您的路径不起作用,您可以尝试将 chromedriver.exe 添加到项目中,就像在此项目结构中一样 .

    chromedriver.exe

    然后你应该在主文件中加载 chromedriver.exe . 至于我,我在 driver.py 中加载了 driver.exe .

    def get_chrome_driver():
    return webdriver.Chrome("..\\content\\engine\\chromedriver.exe",
                                chrome_options='--no-startup-window')
    

    .. 表示 driver.py's 上层目录

    . 表示 driver.py 所在的目录

    希望这会有所帮助 .

相关问题