如何将浏览器参数传递给Watir

use*_*335 4 ruby watir phantomjs

我正在使用Ruby并使用最新的Watir宝石版本.我打算使用像PhantomJS这样的无头浏览器.如何在执行时将paramater传递给Phantom JS浏览器.

我的目的是让Phantom JS不需要加载图像.

Jus*_* Ko 9

PhantomJS命令行页面所述,有一个控制图像加载的选项:

--load-images = [true | false]加载所有内联图像(默认为true).也接受:[是|否].

在Watir :: Browser初始化期间,可以在:args键中将这些设置指定为数组.例如:

args = %w{--load-images=false}
browser = Watir::Browser.new(:phantomjs, :args => args)
Run Code Online (Sandbox Code Playgroud)

一个工作的例子:

require 'watir-webdriver'

# Capture screenshot with --load-images=true
browser = Watir::Browser.new(:phantomjs)
browser.goto 'https://www.google.com'
browser.screenshot.save('phantomjs_with_images.png')

# Capture screenshot with --load-images=false
args = %w{--load-images=false}
browser = Watir::Browser.new(:phantomjs, :args => args)
browser.goto 'https://www.google.com'
browser.screenshot.save('phantomjs_without_images.png')
Run Code Online (Sandbox Code Playgroud)

这给截图加载了图片:

phantomjs_with_images.png

没有加载图片的截图:

phantomjs_without_images.png

请注意,页面的Google图像未在第二个屏幕截图中加载(正如预期的那样,因为load-images设置为false).