Chrome Headless in Unix returns empty page source

我与影子孤独终老i 提交于 2019-12-12 10:36:02

问题


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)

回答1:


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



回答2:


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)    



回答3:


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




回答4:


On Unix you need a reference to the Chrome Binary file too.

options.setChromeBinaryPath("/usr/bin/google-chrome");


来源:https://stackoverflow.com/questions/47715071/chrome-headless-in-unix-returns-empty-page-source

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