0

嗨,我在 Eclipse 中为 java 启动 phantomjs 时出现以下错误 PhantomJS 正在启动 GhostDriver ... 在此处输入图像描述

我已完成以下步骤将 phantomjs 添加到 eclipse :

  1. 下载 phantomjs.exe

  2. 解压 phantomjs-1.8.x-windows.zip 文件夹并将 phantomjs.exe 文件定位到 C:/ 文件夹

  3. 将以下导入添加到您的代码中:

导入 org.openqa.selenium.phantomjs.PhantomJSDriver;导入 org.openqa.selenium.phantomjs.PhantomJSDriverService;导入 org.openqa.selenium.remote.DesiredCapabilities;

将指定“FirefoxDriver”的对象“驱动程序”替换为“PhantomJSDriver”。

替换代码,WebDriver driver = new FirefoxDriver

DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe"); WebDriver driver = new PhantomJSDriver(caps);

运行测试。

请帮忙 !!

4

1 回答 1

2

为了使 GhostDriver 正常工作,请执行以下步骤:

                      PREREQUISITES

先决条件步骤 1:

下载所需的所有内容后,下一步 - 启动 selenium hub(selenium 服务器)和 selenium 节点,该节点将连接到启动的 hub 并将基于 phantomJs。

                   LAUNCHING HUB AND NODES
  1. 硒集线器启动

    java -jar selenium-server-standalone-2.41.0.jar -role hub

启动集线器后 -http://localhost:4444/grid/console 在浏览器中输入本地检查 => http://gyazo.com/9435772d76044cf273d6b567584c0532

  1. GhostDriver 节点启动

    phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://localhost:4444

再次检查 localhost/grid/console => http://gyazo.com/06fd2fc6d740c18e3d0925e180de150f

完成后,进行以下设置。

                               CODE SETUP

方法一

我的项目-基于 maven,所以我建议您遵循 PhantomJs 跨平台解决方案

在 POM.XML 添加以下内容

         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-java</artifactId>
             <version>2.41.0</version>
         </dependency>

         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-support</artifactId>
             <version>2.41.0</version>
         </dependency>


<!--substituting   phanbedder  with local Phanbedder implementation-->
             <dependency>
         <groupId>net.anthavio</groupId>
         <artifactId>phanbedder-1.9.7</artifactId>
         <version>1.0.0</version>
     </dependency>



     <dependency>
         <groupId>com.github.detro.ghostdriver</groupId>
         <artifactId>phantomjsdriver</artifactId>
         <version>1.1.0</version>
     </dependency>

添加这些依赖项后,切换到测试类(例如 SeleniumTest.java)

 private WebDriver driver;

    @BeforeClass
    public void seleniumGrridUponGhostDriver() throws MalformedURLException {


       File phantomjs = Phanbedder.unpack(); // cross platform solution. Maven will provide //appropriate phantomJs instance

        DesiredCapabilities dcaps = new DesiredCapabilities();
        dcaps.setCapability("takesScreenshot", true);


        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());

// NOTE: as we launched node locally, that is why we pass
// 127.0.0.1  IP as parameter
        this.driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080"), dcaps);

        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    }

@Test
public void myTest(){
driver.get("http://www.google.com");
.....

}

方法二

在 POM.xml 中添加以下依赖项

    <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-java</artifactId>
             <version>2.41.0</version>
         </dependency>

         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-support</artifactId>
             <version>2.41.0</version>
         </dependency>

适当的代码设置:

  private WebDriver driver;

    @BeforeClass
    public void seleniumGrridUponGhostDriver() throws MalformedURLException {

        File phantomjs = new File("C:\\Selenium\\phantomjs.exe");


        DesiredCapabilities dcaps = new DesiredCapabilities();
        dcaps.setCapability("takesScreenshot", true);


        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());

        this.driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080"), dcaps);


        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);


    }

    @Test
    public void myTest(){
    driver.get("http://www.google.com");
    .....

    }

希望这对你有用

于 2014-07-06T17:11:33.033 回答