首页 文章

无法通过Selenium Hub获取Chrome

提问于
浏览
1

我正在使用Selenium Webdriver,2.25我有一个本地集线器设置了这个json设置chrome和firefox:

[
    {
        "browserName": "firefox",
        "maxInstances": 5,
        "seleniumProtocol": "WebDriver"
    },
    {
        "browserName": "chrome",
        "maxInstances": 5,
        "seleniumProtocol": "WebDriver"
    }
],

我可以像这样启动一个webdriver firefox会话:

capability = getattr(webdriver.DesiredCapabilities, "FIREFOX")
dd=webdriver.Remote('http://localhost:4444/wd/hub', capability)

哪个工作正常,但如果我尝试启动这样的Chrome会话:

capability = getattr(webdriver.DesiredCapabilities, "CHROME")
dd=webdriver.Remote('http://localhost:4444/wd/hub', capability)

我收到此错误:

回溯(最近一次调用最后一次):文件“”,第1行,文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”,第62行,在init中self.start_session(desired_capabilities,browser_profile)文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”,第104行,在start_session中的“desiredCapabilities”:desired_capabilities,File“/ usr / local / lib / python2.7 / dist-packages / selenium / webdriver / remote / webdriver.py“,第155行,执行self.error_handler.check_response(响应)文件”/usr/local/lib/python2.7 /dist-packages/selenium/webdriver/remote/errorhandler.py“,第147行,在check_response中引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:无; Stacktrace:方法innerGet在None中抛出错误

但我可以像这样开始直接连接到Chrome:

dd=webdriver.Chrome()

没有任何问题 .

如何通过我的Selenium Hub访问Chrome?

2 回答

  • 2

    您需要设置chrome驱动程序,有关该信息的信息here

    UPDATE


    基于sample json setup file和第一个链接中提供的步骤,似乎浏览器名称不应该在Upper中,而实际上是小写 .

    所以将 CHROME 改为 chrome

    WebDriver driver = new RemoteWebDriver("http://localhost:9515", DesiredCapabilities.chrome());
    driver.get("http://www.google.com");
    

    在你的情况下,我会假设

    dd=webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.chrome())
    
  • 1

    我确实遇到了同样的问题 .

    事情是,与Firefox不同,Chrome需要单独的chromdriver.exe作为浏览器和驱动程序之间的桥梁 .

    从文档:

    ChromeDriver由三个独立的部分组成 . 有浏览器本身(“chrome”),Selenium项目提供的语言绑定(“驱动程序”)和从Chromium项目下载的可执行文件,它充当“chrome”和“driver”之间的桥梁 . 此可执行文件称为“chromedriver”,但我们将尝试将其称为此页面中的“服务器”以减少混淆 .

    Download chromdriver.exe here

    并把它放在你的chrome二进制目录中 .

    然后我使用.bat文件来启动我的中心列表:

    java -Dwebdriver.chrome.driver="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" -jar D:\soft\selenium-server-standalone-2.29.0.jar
    

    然后我在我的Linux机器上执行以下Python代码,一旦我将chromedriver.exe放入Chrome目录并使用正确的路径参数启动集线器,它就能完美运行:

    from selenium import webdriver
    url = "http://192.168.1.115:4444/wd/hub"
    driver = webdriver.Remote(command_executor = url, desired_capabilities = {'browserName':'chrome'})
    driver.get("http://google.com")
    

    希望这可以帮助您和其他人遇到同样的问题 . 找到解决方案当然不是将firefox方法视为理所当然和RTFM:Chrome driver documentation

相关问题