首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unix中的Chrome Headless返回空页面源代码

Unix中的Chrome Headless返回空页面源代码
EN

Stack Overflow用户
提问于 2017-12-08 21:09:19
回答 5查看 7.3K关注 0票数 8

我使用Chrome Headless在Unix机器(Cent OS).But中运行我的selenium测试脚本,相同的脚本在我本地的windows机器上运行得很好。

但在Unix机器中,它返回的页面源代码是空的,就像空的html标签一样。

虽然我使用的是最新的ChromeDriver 2.33和谷歌chrome版本62.0,但我不知道它的错误出在哪里。

代码语言:javascript
复制
       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..");

在Unix机器上运行上面的代码,我得到了

页面来源:

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

帮我把这个修好谢谢

完成StackTrace:

代码语言:javascript
复制
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)
EN

回答 5

Stack Overflow用户

发布于 2017-12-08 21:19:28

要在Headless Mode中运行Unix Systems中的Chrome Browser,请添加参数--disable-gpu--remote-debugging-port=9222

代码语言:javascript
复制
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..");

注意: A:避免为任何单个WebDriver实例同时传递选项addArguments("--headless")setHeadless(true)

备注B:避免在基于Linux/Unix的系统上使用addArguments("--disable-gpu");,因为它是基于Windows的操作系统的配置。

参考:

更新

正如你所看到的:

代码语言:javascript
复制
WebDriverException: chrome not reachable

请按照以下步骤操作:

通过Revo Uninstaller

  • Run tool卸载
  • Chrome Browser可清除所有操作系统杂务。
  • 使用当前版本的Revo Uninstaller
  • Run System Restart
  • Install Chrome Browser.
  • Execute Test
票数 2
EN

Stack Overflow用户

发布于 2018-07-10 02:38:25

我也有同样的问题。特别是在添加没有有效证书的代理时。下面是对我有效的方法:(python代码)

代码语言:javascript
复制
    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)    
票数 1
EN

Stack Overflow用户

发布于 2018-03-01 06:03:04

这是ssl证书的问题,我也有同样的问题。对于无头镀铬以下选项无效,请尝试访问其他非ssl链接。chrome选项->--忽略-ssl-错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47715071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档