【问题标题】:How to run ghostdriver with Selenium using java如何使用 Java 使用 Selenium 运行 ghostdriver
【发布时间】:2012-12-16 19:52:31
【问题描述】:

我想使用phantomJS 进行一些网络测试,我遇到了GhostDriver (https://github.com/detro/ghostdriver)。我已经使用自述文件中的说明构建了它,并且可以在指定的端口上运行它,但我不确定如何从我的 java 代码中访问 Web 驱动程序。为了澄清,我在 ruby​​ 中看到了这个例子:

  caps = {
  :browserName => "phantomjs",
  :platform => "LINUX"
   }

urlhub = "http://key:secret@hub.testingbot.com:4444/wd/hub"

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120

@webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
@webdriver.navigate.to "http://www.google.com/"
puts @webdriver.title
@webdriver.save_screenshot("./screenshot.png")
@webdriver.quit

我只是不确定如何在 java 中做同样的事情。

【问题讨论】:

    标签: java selenium-webdriver webdriver phantomjs ghostdriver


    【解决方案1】:

    只是为了澄清其他可能看到的人,从 java 运行它:

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                    "/Path/to/bin/phantomjs");                  
    driver = new PhantomJSDriver(caps);
    

    然后就可以像普通的WebDriver一样使用了。

    【讨论】:

    • “/Path/to/bin/phantomjs”是什么意思,没看懂,请解释一下。
    • 只要使用 phantomJS 二进制文件的路径即可。
    • 我在 Spring MVC 中运行整个程序,我得到了SEVERE: Servlet.service() for servlet [spring] in context with path [/my_spring_app] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/google/common/base/Function] with root cause。你知道如何解决这个问题吗?
    • @PASTRY 您需要将Google Guava 添加到类路径中。这是 selenium-java 的运行时依赖。
    • 将PhantomJS的可执行路径添加到系统路径就不用传了。
    【解决方案2】:

    我相信这个链接会回答你的问题。您将需要 Selenium 2.28.0 和 PhantomJS 1.8。我已经对此进行了测试,它的工作原理与宣传的一样,尽管我的测试是先行的。请注意,您需要下载 Selenium zip 文件以获取包含绑定的 jar。 Maven 存储库尚未包含它。

    http://ivandemarino.me/2012/12/04/Finally-GhostDriver-1-0-0/

    【讨论】:

    • 是的,它现在可以工作了。不清楚您是否需要 selenium 2.28。谢谢!
    • 链接现在不存在。
    【解决方案3】:

    首先下载PhantomJSDriver的exe文件。不需要安装,只需要从http://phantomjs.org/download.html下载这个文件,并在给定的代码中简单地给出exe文件的路径。

     public class Browserlaunch {
        public static void main(String[] args) {
            DesiredCapabilities DesireCaps = new DesiredCapabilities();
            DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/Drivers/phantomjs/bin/phantomjs.exe");
            WebDriver driver=new PhantomJSDriver(DesireCaps);
            driver.get("http://google.com");
    
       }
    }
    

    【讨论】:

      【解决方案4】:

      只设置系统属性:

      System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
      WebDriver driver = new PhantomJSDriver();
      

      【讨论】:

        猜你喜欢
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        相关资源
        最近更新 更多