8

我正在尝试无头运行 Selenium(没有出现浏览器)。其他问题已指出xvfb作为执行此操作的工具。但是,它似乎非常不稳定,一直在崩溃,所以我正在寻找另一种选择。

是否有非 xvfb 无头运行 Selenium 的方式?

4

3 回答 3

13

我认为不运行 X 服务器就无法运行浏览器。

如果您不喜欢 Xvfb,那么正如 Pascal 所说,您最好的选择可能是运行 VNC 服务器——我个人喜欢Xtightvnc。这意味着您正在运行一个(无头)X 服务器,您可以随时通过 VNC 进入该服务器,以防万一出现问题并且您想查看它。我总是有一个 VNC 服务器在运行,并且我正在使用指向该服务器的 $DISPLAY 环境变量运行我的测试。

(有人对我投了反对票,所以也许我应该澄清一下:像 Xtightvnc 这样的 X11 VNC 服务器与 Windows 或 OS X 上通常的 VNC 服务器不同,后者只会在网络上共享您现有的屏幕。不要混淆。;-))

于 2011-03-12T22:17:43.367 回答
6

我很惊讶。我已经多次使用 Selenium 和 Xvfb 没有任何问题,许多其他用户也在这样做。您能否更具体地了解您的设置和面临的问题?你如何启动 Xvfb?你能提供xvfb.log吗?

但是,要回答您的问题,可以使用 X VNC 服务器。例如,请参阅此页面以获取一些说明。如果没有有关您的配置的任何详细信息,实际上很难更精确。

于 2009-12-27T17:30:15.687 回答
1

使用 --headless 运行 chrome 浏览器,还可以减少资源使用。使用 ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox") 来实现它。该方案假设安装了 Chrome 浏览器和 Chromedriver。

这是我在 Jenkins 工作中使用的简单 Selenium java 测试

    package com.gmail.email;

import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class FirstTest {
    private static ChromeDriver driver;
    WebElement element;

    @BeforeClass
    public static void openBrowser(){

        ChromeOptions ChromeOptions = new ChromeOptions();
        ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox");
        driver = new ChromeDriver(ChromeOptions);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test // Marking this method as part of the test
    public void gotoHelloWorldPage() {
        // Go to the Hello World home page
        driver.get("http://webapp:8080/helloworld/");

        // Get text from heading of the Hello World page
        String header = driver.findElement(By.tagName("h2")).getText();
        // Verify that header equals "Hello World!"
        Assert.assertEquals(header, "Hello World!");

    }

    @AfterClass
    public static void closeBrowser(){
        driver.quit();
    }
}

更多细节在这里 https://github.com/SeleniumHQ/docker-selenium/issues/429

于 2018-03-14T09:48:23.923 回答