无头火狐和Selenium

时间:2014-02-12 19:41:31

标签: firefox selenium

我尝试用无头火狐运行Selenium代码。在Eclipse中我输入了这段代码:

String Xport = System.getProperty(
            "lmportal.xvfb.id", ":10");
    final File firefoxPath = new File(System.getProperty(
            "lmportal.deploy.firefox.path", "/usr/bin/firefox/"));
    FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
    firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);

    // Start Firefox driver
    WebDriver driver = new FirefoxDriver(firefoxBinary, null);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

但到达时:

        WebDriver driver = new FirefoxDriver(firefoxBinary, null);

没有前进..

我尝试使用phantomjs但非常慢......

1 个答案:

答案 0 :(得分:0)

是肯定的。你可以通过Xvbf用无头火狐运行selenium。你可能有以下步骤:

  • 安装Xvbf,如 yum install xorg-x11-server-Xvfb.x86_64
  • 启动Xvbf并设置DISPLAY编号 nohup Xvfb:1-screen 0 1024x768x24&
  • 在您的代码中,它可能就像

    FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);  firefoxBinary.setEnvironmentProperty(“DISPLAY”,1);