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?

3 Answers 3

15

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. ;-) )

Sign up to request clarification or add additional context in comments.

2 Comments

excellent answer -- especially since RHEL doesn't seem to have Xvfb in the yum repositories anymore. tigervnc is there, however. So I would say this is a much more officially supported solution, yet googling seems to lead you first to xvfb...
Cool, but some users may be at a loss as to how to use it. Could you add some examples? something like: run $ vncserver, check the output index New 'X' desktop is host:1 and then use it to run your command, for example for xcalc $ DISPLAY=:1 xcalc. You can also see that's going on that display: $ vncviewer localhost:59XX
6

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 Comments

I guess xvfb's problem was this: wiki.maemo.org/…. xvfb always felt like a weird solution, so I had been looking for another method anyway (although VNC feels the same :-/). Thanks.
If you found the problem, then I guess you found the solution, didn't you?
I thought so. I applied to fix, it looked fine, but its still broken. bugs.launchpad.net/ubuntu/+source/xorg-server/+bug/330052 looks like the problem.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.