如何在selenium服务器独立运行任何(Chrome,Firefox,PhantomJS)浏览器?

时间:2017-02-09 09:00:09

标签: selenium selenium-chromedriver selenium-server

我的问题或问题=我无法使用selenium远程服务器运行任何浏览器,我该如何解决?

我的环境:

  • 操作系统Windows 10
  • 我正在使用eclipse(版本:Neon Release(4.6.0))和java 1.8
  • selenium Web驱动程序3.0.0
  • 硒 - 服务器 - 独立-3.0.1.jar

我在cmd中启动selenium-server-standalone。 (“selenium-server-standalone-3.0.1.jar”文件存储在我的c盘上的utilities文件夹中)

C:\Windows\system32> cd\
C:\> cd utilities
C:\Utilities> java -jar selenium-server-standalone-3.0.1.jar

enter image description here

然后selenium-server-standalone启动,一切正常

enter image description here

当我运行测试时

Eclipse提供了此错误:

Feb 09, 2017 10:36:35 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Feb 09, 2017 10:36:35 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to original OSS JSON Wire Protocol.
Feb 09, 2017 10:36:36 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Falling back to straight W3C remote end connection
org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}], required capabilities = Capabilities [{}]
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'MWLTSHAUNCR', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_45'
Driver info: driver.version: RemoteWebDriver

在cmd中我收到此错误: enter image description here

对于我添加的remoteDriver部分,我的代码看起来像这样。我已经包含了类和构造函数

public class browser {
    private  browser (WebDriver driver){ 
        browser.driver = driver;
    }

    public static void runRemoteDriver(){
        try {
            WebDriver webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.firefox());
            new browser (webDriver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}   

2 个答案:

答案 0 :(得分:2)

我将所有驱动程序复制到我存储“selenium-server-standalone-3.0.1.jar”文件的文件夹中。然后它工作!!!

enter image description here

我使用cmd命令启动了服务器:

java -jar selenium-server-standalone-3.0.1.jar

然后在您的代码中,您必须指定要运行的浏览器,例如:

    WebDriver webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.firefox());
    WebDriver webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.chrome());
    WebDriver webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.phantomjs());

答案 1 :(得分:2)

我认为您缺少driver.exe文件的路径。您有两种选择:

1.您可以像通常那样使用 System.setProperty()

2.您可以使用如下所示的路径启动RemoteDriver。

java -Dphantomjs.binary.path=phantomjs.exe -jar selenium-server-standalone-3.4.0.jar

并且这行代码负责建立连接。

WebDriver webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.firefox());