Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Headless in Unix returns empty page source

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)
like image 899
Sivaram Kumar Avatar asked Dec 08 '17 13:12

Sivaram Kumar


People also ask

How do I run Chrome headless in Linux?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.

How do I run Chrome in headless mode?

As we have already seen, you just have to add the flag –headless when you launch the browser to be in headless mode. With CLI (Command Line Interface), just write: chrome \<br> – headless \ # Runs Chrome in headless mode. <br> – disable-gpu \ # Temporarily needed if running on Windows.

What is difference between Chrome and Chrome headless?

Headless Chrome is a way to run the Chrome browser in a headless environment without the full browser UI. One of the benefits of using Headless Chrome (as opposed to testing directly in Node) is that your JavaScript tests will be executed in the same environment as users of your site.

Does Chromium support headless?

Headless Chromium allows running Chromium in a headless/server environment. Expected use cases include loading web pages, extracting metadata (e.g., the DOM) and generating bitmaps from page contents -- using all the modern web platform features provided by Chromium and Blink.


2 Answers

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
like image 120
undetected Selenium Avatar answered Oct 05 '22 22:10

undetected Selenium


I had the same issue. Especially when adding the proxy which had no valid certificates. Here's what worked for me: (python code)

    polipo_proxy = "127.0.0.1:8124"
    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': polipo_proxy,
        'ftpProxy' : polipo_proxy,
        'sslProxy' : polipo_proxy,
        'noProxy'  : ''
    })
    capabilities = dict(DesiredCapabilities.CHROME)
    proxy.add_to_capabilities(capabilities)
    capabilities['acceptSslCerts'] = True
    capabilities['acceptInsecureCerts'] = True

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--window-size=1280,1000')
    chrome_options.add_argument('--allow-insecure-localhost')
    chrome_options.add_argument('--allow-running-insecure-content')
    chrome_options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(executable_path=os.path.abspath("/var/chromedriver/chromedriver"), chrome_options=chrome_options, desired_capabilities=capabilities)    
like image 21
Michael Michelis Avatar answered Oct 06 '22 00:10

Michael Michelis