如何在无头Chrome上使用Selenium Webdriver?

时间:2017-06-07 23:14:04

标签: ruby selenium selenium-webdriver selenium-chromedriver headless-browser

我正在学习使用Selenium来做基本的事情,例如截取屏幕,抓取和测试,并希望将它与无头Chrome一起使用,Chrome现在从Chrome 59开始就是稳定的。

我已经能够使用' selenium-webdriver'宝石和chromedriver,但不是无头。

这是我正在运行的ruby脚本,它在开始初始化驱动程序后挂起

require 'rubygems'
require 'selenium-webdriver'

Selenium::WebDriver.logger.level = :debug
p 'initializing driver'
driver = Selenium::WebDriver.for :chrome, switches: %w[--headless --disable-gpu --screenshot --hide-scrollbars]
p 'navigating to Google'
driver.navigate.to "http://google.com"  
driver.save_screenshot("./screen.png")
driver.quit

和日志的输出:

:> ruby rubytest.rb
"initializing driver"
2017-06-07 15:55:43 DEBUG Selenium Executing Process 

["/Users/name/Documents/scrapings/python/env/bin/chromedriver", "--port=9515"]
2017-06-07 15:55:43 DEBUG Selenium polling for socket on ["127.0.0.1", 9515]
Starting ChromeDriver 2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b) on port 9515
Only local connections are allowed.
2017-06-07 15:55:43 INFO Selenium -> POST session
2017-06-07 15:55:43 INFO Selenium    >>> http://127.0.0.1:9515/session | {"desiredCapabilities":{"browserName":"chrome","version":"","platform":"ANY","javascriptEnabled":true,"cssSelectorsEnabled":true,"takesScreenshot":false,"nativeEvents":false,"rotatable":false,"chromeOptions":{"args":["--headless","--disable-gpu","--screenshot","--hide-scrollbars"]}}}
2017-06-07 15:55:43 DEBUG Selenium      > {"Accept"=>"application/json", "Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>"284"}
[RUBY BACKTRACE TO DRIVER INITIALIZATION]

我尝试使用类似代码的JavaScript和Python驱动程序,但没有任何作用。当我使用Python尝试此操作时,错误消息是

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.5 x86_64)

3 个答案:

答案 0 :(得分:5)

我发现this blog post对于在红宝石中使用硒设置无头铬很有用

require "selenium-webdriver"

# configure the driver to run in headless mode
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
driver = Selenium::WebDriver.for :chrome, options: options

driver.navigate.to "https://www.google.com"

# resize the window and take a screenshot
driver.manage.window.resize_to(800, 800)
driver.save_screenshot "screenshot.png"

答案 1 :(得分:2)

通过各种文档,博客文章和要点,最终管理完成此工作。

caps = Selenium::WebDriver::Remote::Capabilities.chrome("desiredCapabilities" => {"takesScreenshot" => true}, "chromeOptions" => {"binary" => "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"})

driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps, switches: %w[--headless --no-sandbox --disable-gpu --remote-debugin-port=9222 --screen-size=1200x800]

您需要使用最新版本的Chrome(我使用Canary)并告诉Selenium二进制文件的路径。您还需要为“截屏”设置所需的功能。为真。

答案 2 :(得分:0)

我已经写过关于如何做的blog post。总结:

1)确保您在Linux上 Chrome 57 + ,在macOS上 59 + 60 + 在Windows上(最后一个还没有发布,你必须使用测试版,又名" Canary");

2)添加/更新gem selenium-webdriver;

3)确保您使用的是 ChromeDriver 版本2.30或更高版本;

4)将以下驱动程序添加到spec_helper.rbrails_helper.rb

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new app, browser: :chrome,
    options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu])
end

Capybara.javascript_driver = :chrome