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

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

我已经能够使用’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) 

我发现这篇博客文章对于在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" 

最终通过各种文档,博客文章和要点来管理这项工作。

 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’的所需function设置为true。

我写了一篇关于如何做的博客文章 。 加起来:

1)确保你在Linux上有Chrome版本57+ ,在macOS上有59+或在Windows上有60+ (最后一个还没有发布,你必须使用beta,也就是“Canary”);

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

3)确保您使用的是Chrome驱动程序 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 
Interesting Posts