Chrome Headless in Unix returns empty page source Chrome Headless in Unix returns empty page source selenium selenium

Chrome Headless in Unix returns empty page source


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


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)    


It's problem with ssl certificate , i also had the same issue . for headless chrome following options are not effective , try to reach other non ssl link . chrome options - > --ignore-ssl-errors