Java – Chrome Headless in Unix returns empty page source

google-chromegoogle-chrome-headlessjavaseleniumunix

Am using Chrome Headless to run my selenium test script in Unix machine(Cent OS).But the same script works perfectly in my local windows machine.

But in Unix machine it returns empty page source like empty html tags.

Have no clue where its error-ed out though am using latest ChromeDriver 2.33, and google chrome version 62.0..

       System.setProperty("webdriver.chrome.driver", "/../chromedriver.exe");

        --chromedriver.exe for windows local machine
        -- chromedriver for unix machine

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--no-sandbox");
        options.setAcceptInsecureCerts(true);
        options.addArguments("--ignore-ssl-errors=true");
        options.addArguments("--ssl-protocol=any");
        options.setHeadless(true);

        driver = new ChromeDriver(options);

        System.out.println("Timeout invoke ");
        driver.get("https://www.google.co.in/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println("After invoke ");
        System.out.println("PAGE SOURCE : \n" + driver.getPageSource());
        System.out.println("RUN COMPLETE..");

Running the above in Unix machine i get

PAGE SOURCE:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

Help me in fixing this
Thanks in advance

Complete StackTrace :

Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 33523 
Only local connections are allowed. org.openqa.selenium.WebDriverException: chrome not reachable   (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT
6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) 

Command duration or timeout: 60.41 seconds Build info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:01:39.354Z' System info: host: 'Windows', ip: '', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144' 

Driver info: driver.version: ChromeDriver   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)   at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)     at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$new$0(JsonWireProtocolResponse.java:53)   at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$getResponseFunction$2(JsonWireProtocolResponse.java:91)   at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)  at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)    at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)     at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)  at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)     at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)     at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)    at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)     at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)   at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)     at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)  at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)     at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:219)    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)  at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)

Best Answer

To run Chrome Browser in Headless Mode in Unix Systems, add the arguments --disable-gpu and --remote-debugging-port=9222 :

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.setAcceptInsecureCerts(true);
options.addArguments("--ignore-ssl-errors=true");
options.addArguments("--ssl-protocol=any");
options.setHeadless(true);
options.addArguments("--remote-debugging-port=9222");
options.addArguments("window-size=1400,600");
WebDriver driver = new ChromeDriver(options);
System.out.println("Timeout invoke ");
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("After invoke ");
System.out.println("PAGE SOURCE : \n" + driver.getPageSource());
System.out.println("RUN COMPLETE..");

Note A: Avoid passing both the options addArguments("--headless") and setHeadless(true) together for any single WebDriver instance.

Note B: Avoid using addArguments("--disable-gpu"); on Linux/Unix based systems as it is a configuration for Windows based OS.

Reference : Getting Started with Headless Chrome


Update

As you are seeing :

WebDriverException: chrome not reachable

Follow the steps :

  • Uninstall Chrome Browser through Revo Uninstaller
  • Run CCleaner tool to wipe off all the OS chores.
  • Take a System Restart
  • Install current version of Chrome Browser.
  • Execute your Test
Related Topic