【问题标题】:Running Selenium WebDriver python bindings in chrome在 chrome 中运行 Selenium WebDriver python 绑定
【发布时间】:2012-01-05 13:20:46
【问题描述】:

我在使用 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 来解决问题:

铬 --remote-shell-port=9222

但是,这也不起作用。

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

www-client: 铬 15.0.874.121
开发语言:python 2.7.2-r3 Selenium 2.11.1
操作系统:GNU/Linux Gentoo 内核 3.1.0-gentoo-r1

【问题讨论】:

  • 我只是想让你知道“--remote-shell-port”不是一个有效的命令行选项。请参阅:peter.sh/experiments/chromium-command-line-switches(“--remote-debugging-port”将是正确的。请注意,您需要在使用该选项之前关闭所有铬实例。即在现有浏览器会话中创建新窗口不起作用)。

标签: python linux google-chrome selenium automated-tests


【解决方案1】:

仅限 Mac OSX

一个更简单的开始方法(假设你已经安装了homebrew,如果没有,你应该先这样做,让自制软件让你的生活变得更好)是运行以下命令:

brew install chromedriver

这应该将 chromedriver 放在你的路径中,你应该已经准备好了。

【讨论】:

  • 它不能在 Mac 上运行。 selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.
  • @wyx 您的环境可能设置不正确。尝试运行brew doctor 看看是否有任何发现。
  • 当您为拥有一台 Mac 而感到高兴时,这是一个温暖而模糊的时刻。这么简单。
  • 心!我爱酿造
【解决方案2】:

适用于 Linux

  1. 检查你是否安装了最新版本的chrome brwoser-> chromium-browser -version

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

  3. here获取合适版本的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()
    

【讨论】:

  • 非常好!注意:虽然我运行的是 64 位 linux,但我只能使用 32 位 chromedriver。
  • 在 Ubuntu (14.04) 上,您可以安装 chromium-chromdriver 软件包(例如使用 apt-get)并将 chromedriver 文件的路径添加到 PATH shell 变量 PATH="${PATH}":/ usr/lib/chromium-browser/ 如果你把它添加到你的 .bashrc 中,你就不需要在每次使用 selenium 测试时都设置它。
  • 实际上这个答案应该有更多的赞成票。基本上,对于像 geckodriver 这样的每个司机来说,这都是要走的路。
  • 我的 chromium-browser 版本被称为 (Chromium 67.0.3396.99 Built on Ubuntu , running on Ubuntu 16.04) 但该链接只有 2.0-2.9 版本。需要一些帮助。
  • 请删除 display.stop() @Vicky,它不起作用并给出 ab 错误。顺便说一句,谢谢。愿上帝保佑你。
【解决方案3】:

对于窗户

this direct link 下载 ChromeDriver this page 获取最新版本。

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

现在应该可以工作了:

from selenium import webdriver
driver = webdriver.Chrome()

【讨论】:

    【解决方案4】:

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

    先决条件:https://sites.google.com/a/chromium.org/chromedriver/downloads下载最新的 Chrome 驱动程序

    方式 1:

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

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

    方式 2:

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

    self.driver = webdriver.Chrome()
    

    【讨论】:

      【解决方案5】:

      对于 Windows 的 IDE:

      如果您的路径不起作用,您可以尝试将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所在的目录

      希望这会有所帮助。

      【讨论】:

        【解决方案6】:

        对于 Windows,请将chromedriver.exe 放在&lt;Install Dir&gt;/Python27/Scripts/

        【讨论】:

        • 在哪里放 ubuntu ? int /usr/lib/ 或其他地方?
        【解决方案7】:

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

        【讨论】:

        • 抱歉忘记提了。我得到了这个链接。
        • 尝试了 Chromedriver 而不是 Firefox Webdriver.. 性能提升近 3 倍..
        • 另外,只想提一下,传递给构造函数的参数是 ChromeDriver binary 的路径,而 不是 包含ChromeDriver 二进制文件(浪费了过去半个小时搞清楚出了什么问题)。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-08
        • 2014-07-12
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 2017-02-28
        • 1970-01-01
        相关资源
        最近更新 更多