代码之家  ›  专栏  ›  技术社区  ›  Paul Biggar

在不使用XVFB的情况下运行Selenium Headless

  •  8
  • Paul Biggar  · 技术社区  · 15 年前

    我正在尝试运行Selenium Headless(不显示浏览器)。其他问题已经指出 xvfb 作为这样做的工具。然而,它看起来非常不稳定,总是崩溃,所以我正在寻找另一种选择。

    是否有非XVFB方式运行Selenium Headless?

    3 回复  |  直到 7 年前
        1
  •  13
  •   Jo Liss    14 年前

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

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

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

        2
  •  6
  •   Sam    10 年前

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

    但是,要回答您的问题,可以使用XVNC服务器。参见例如 this page 为了一些指示。实际上,如果没有配置的任何细节,就很难更加精确。

        3
  •  0
  •   LocalOps    7 年前

    使用--headless运行chrome浏览器,还可以减少资源使用。 chromeoptions.addarguments(“--headless”,“window size=1024768”,“--no sandbox”)。 去实现它。此方案假定已安装Chrome浏览器和Chromedriver。

    以下是我在詹金斯工作中使用的简单的SeleJava测试

        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