web-dev-qa-db-ja.com

ヘッドレスChromeでSeleniumWebdriverを使用するにはどうすればよいですか?

スクリーンショットの撮影、スクレイピング、テストなどの基本的な目的でSeleniumを使用する方法を学んでおり、Chrome 59の時点で安定しているヘッドレスChromeで使用したいと考えています。

'Selenium-webdriver' gemと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)
7

私は このブログ投稿 ヘッドレスのセットアップに役立ちますchrome RubyのSeleniumで

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"
7
acowpy

最終的には、さまざまなドキュメント、ブログ投稿、要点を介してこれを処理することができました。

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にバイナリへのパスを指示する必要があります。また、 'takesScreenshot'に必要な機能をtrueに設定する必要があります。

2

私はそれを行う方法について ブログ投稿 を書きました。要約:

1)LinuxにChromeバージョン57 +があることを確認しますmacOSでは59 +、Windowsでは60 +(最後の1つはまだリリースされていませんが、ベータ版、別名「カナリア」を使用する必要があります。

2)gemを追加/更新しますSelenium-webdriver;

3)ChromeDriverバージョン2.30以降を使用していることを確認してください。

4)次のドライバーspec_helper.rbまたはRails_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
1
Lucas Caton