首页 文章

如何使用Selenium连接Chromium Headless

提问于
浏览
11

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

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

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

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

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

我确实得到以下注销

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

问题是:

5 回答

  • 1

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

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

    • 然后你告诉chromedriver使用铬而不是铬

    • 添加"--headless"作为附加参数

    代码应如下所示:

    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上为我工作 .

  • 13

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

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

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

    Example Project on Chrome Headless running with different options

    options.setHeadless(true)
    
  • 0

    *使用以下代码:

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

    你会得到“无头”Chrome!

    *完整的代码

    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 
    
    
        }
    
  • 4

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

相关问题