Java 如何使用 Selenium 连接到 Chromium Headless

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41836422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:42:51  来源:igfitidea点击:

How to connect to Chromium Headless using Selenium

javaseleniumchromiumheadless

提问by geri-m

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

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

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

我确实已经在 9222 上运行了无头版本。所以如果我打开http://10.252.100.33:9222/json/我确实得到

[ {
   "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 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

Questions are:

问题是:

采纳答案by hkq

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).

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

  • Start chromedriver (e.g. ./chromedriveror ./chromedriver --port=9515)
  • Then you have tell the chromedriver to use Chromium instead of Chrome
  • Add --headlessas an additional argument
  • 启动 chromedriver(例如./chromedriver./chromedriver --port=9515
  • 然后你告诉 chromedriver 使用 Chromium 而不是 Chrome
  • 添加--headless作为附加参数

Code should look like this:

代码应如下所示:

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);

Worked for me on Ubuntu Linux.

在 Ubuntu Linux 上为我工作。

回答by Anjan Mondal

Chrome 59 has the ability to create instance as headless . I have tried for Windows with new chrome driver 2.30 and it worked for me https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html?m=1

Chrome 59 能够将实例创建为 headless 。我已经尝试过使用新的 chrome 驱动程序 2.30 的 Windows,它对我 有用 https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html?m=1

回答by Stephen

Alternatively if your running it locally you can just do it like this. In scala.

或者,如果您在本地运行它,您可以这样做。在斯卡拉。

val chromeOptions = new ChromeOptions
chromeOptions.addArguments("--headless")
new ChromeDriver(chromeOptions)

回答by Shantonu

if you are using selenium 3+ chrome driver , you can simply use chrome options and initiate driver. Check details in a project

如果您使用 selenium 3+ chrome 驱动程序,您可以简单地使用 chrome 选项并启动驱动程序。检查项目中的详细信息

Example Project on Chrome Headless running with different options

使用不同选项运行 Chrome Headless 的示例项目

 options.setHeadless(true)

回答by kuo chang

*Use the following code:

*使用以下代码:

ChromeOptions options = new ChromeOptions();  
options.setHeadless(true); //Set Chrome option
driver = new ChromeDriver(options);  

and you will get "Headless" Chrome!

您将获得“无头”Chrome!

*Full code

*完整代码

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;  //import ChromeOptions

public class web_crawl {

    private static WebDriver driver = null;

    public static void main(String[] args) {


       ChromeOptions options = new ChromeOptions();
       options.setHeadless(true);

       driver = new ChromeDriver(options);   
       driver.get("http://www.google.com");   //The website you want to connect to 


    }