使用Chrome Headless在Unix机器(Cent OS)中运行我的selenium测试脚本。但是相同的脚本在我的本地Windows机器上完美运行。
但是在Unix机器上它返回空页面源,就像空的html标签一样。
虽然使用了最新的ChromeDriver 2.33和谷歌浏览器版本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..");
在Unix机器上运行上面的内容我
PAGE SOURCE:
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
帮我解决这个问题 提前致谢
完成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)
答案 0 :(得分:2)
要在Chrome Browser
的{{1}}中投放Headless Mode
,请添加参数 Unix Systems
和 --disable-gpu
:
--remote-debugging-port=9222
注意A :避免将任何单个 WebDriver 实例同时传递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..");
和addArguments("--headless")
选项。
注意B :避免在基于Linux / Unix的系统上使用setHeadless(true)
,因为它是基于Windows的操作系统的配置。
正如您所见:
addArguments("--disable-gpu");
按照以下步骤操作:
WebDriverException: chrome not reachable
Chrome Browser
Revo Uninstaller
工具以清除所有操作系统。CCleaner
System Restart
。Chrome Browser
答案 1 :(得分:1)
我有同样的问题。特别是在添加没有有效证书的代理时。这是对我有用的东西:(python代码)
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)
答案 2 :(得分:0)
ssl证书的问题,我也有同样的问题。对于无头镀铬以下选项无效,请尝试达到其他非ssl链接。 chrome选项 - &gt; - 忽略SSL的错误
答案 3 :(得分:0)
我有同样的问题。事实证明,在我的情况下,请求的页面返回了不成功的HTTP状态代码(在我的情况下为4xx),这在使用<html><head></head><body></body></html>
时导致了getPageSource
。
答案 4 :(得分:-1)
在Unix上,您还需要引用Chrome二进制文件。
options.setChromeBinaryPath("/usr/bin/google-chrome");