Running Selenium headless without using xvfb

邮差的信 提交于 2019-12-03 07:44:10

问题


I'm trying to run Selenium headless (without the browser appearing). Other questions have pointed to xvfb as the tool to do this. However, it appears highly unstable, crashing all the time, so I'm looking for another alternative.

Is there a non-xvfb way of running Selenium headless?


回答1:


I don't think you'll be able to run a browser without running an X server.

If you don't like Xvfb, then as Pascal said, your best bet might be to run a VNC server -- I personally like Xtightvnc. This means you're running a (headless) X server that you can VNC into at any time, in case things go wrong and you want to look at it. I always have a VNC server running, and I'm running my tests with the $DISPLAY environment variable pointing to that server.

(Someone's downvoted me, so maybe I should clarify: X11 VNC servers like Xtightvnc are not the same as the usual VNC servers on Windows or OS X, which would simply share your existing screen on the network. Do not confuse. ;-) )




回答2:


I'm surprised. I've used Selenium and Xvfb several times without any problems and many others users are doing so too. Can you be more specific about your setup and the problems you are facing? How do you start Xvfb? Can you provide xvfb.log?

However, to answer your question, it is possible to use an X VNC server. See for example this page for some instructions. It's actually hard to be more precise without any details about your configuration.




回答3:


Run chrome browser with --headless, also it allows you to reduce resource usage.Use ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox") to achieve it. This scheme assumes installed the Chrome browser and Chromedriver.

Here is my simple Selenium java test that is use in my Jenkins job

    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();
    }
}

More details here https://github.com/SeleniumHQ/docker-selenium/issues/429



来源:https://stackoverflow.com/questions/1966371/running-selenium-headless-without-using-xvfb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!