如何使用硒连接到Chromium Headless [英] How to connect to Chromium Headless using Selenium

查看:85
本文介绍了如何使用硒连接到Chromium Headless的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将无头铬用于使用硒的自动化测试。 ( https://chromium.googlesource.com/chromium/src/ + / lkgr / headless / README.md

I would like to use chromium headless for automated testing using selenium. (https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md)

我确实已经在9222上运行了无头版本。因此,如果我打开 http://10.252.100.33:9222/json/I 确实获得

I do have the headless version already running on 9222. So if i open http://10.252.100.33:9222/json/I do get

[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91",
   "id": "0261be06-1271-485b-bdff-48e443de7a91",
   "title": "The Chromium Projects",
   "type": "page",
   "url": "https://www.chromium.org/",
   "webSocketDebuggerUrl": "ws://127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91"
} ]

As下一步,我想将硒连接到无头铬上。但是当我尝试

As a next step I'd like to connect selenium to the headless chromium. But when i try

final DesiredCapabilities caps = DesiredCapabilities.chrome();
final WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9222/json"), caps);
driver.get("http://www.google.com");

我确实得到了以下注销信息

I do get the following logout

Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Falling back to original OSS JSON Wire Protocol.
Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Falling back to straight W3C remote end connection

org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{browserName=chrome, version=, platform=ANY}], required capabilities = Capabilities [{}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'Geralds-MacBook-Pro.local', ip: '192.168.0.249', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.2', java.version: '1.8.0_111'
Driver info: driver.version: RemoteWebDriver

问题是:

  • Is the RemoteWebDriver the correct driver to connect to the headless chromium?
  • I read about the DevTool protocol (https://docs.google.com/presentation/d/1gqK9F4lGAY3TZudAtdcxzMQNEE7PcuQrGu83No3l0lw/), but I'm not sure, how to create such a client using selenium.
  • Connecting the Chromium Headless using the Chrome DevTools works (https://developers.google.com/web/tools/chrome-devtools/remote-debugging/) besides some segmentation vaults ;-)

推荐答案

我认为自述文件是有点误导。您不必自己启动Chromium,也可以使用 RemoteWebDriver 。确保已安装chromedriver( https://sites.google.com/ a / chromium.org / chromedriver / home )。

I think the readme is a little bit misleading. You don't have to start Chromium itself and you can use the RemoteWebDriver. Make sure that a chromedriver is installed (https://sites.google.com/a/chromium.org/chromedriver/home).


  • 启动chromedriver(例如 ./ chromedriver 。 / chromedriver --port = 9515

  • 然后,您告诉chromedriver使用Chromium而不是Chrome

  • 添加-无头作为附加参数

  • Start chromedriver (e.g. ./chromedriver or ./chromedriver --port=9515)
  • Then you have tell the chromedriver to use Chromium instead of Chrome
  • Add --headless as an additional argument

代码应如下所示:

final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/usr/bin/chromium-browser");
chromeOptions.addArguments("--headless");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new RemoteWebDriver(url, desiredCapabilities);

在Ubuntu Linux上为我工作。

Worked for me on Ubuntu Linux.

这篇关于如何使用硒连接到Chromium Headless的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆