本文是小编为大家收集整理的关于如何将浏览器参数传递给Watir的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我正在使用Ruby,并且正在使用最新的GEM版本Watir.我计划使用无头浏览器,例如phantomjs.执行时,如何将Paramater传递到Phantom JS浏览器.
我的目的是使幻影JS不需要加载图像.
推荐答案
如 phantomjs命令行页面控制图像的加载:
- load-images = [true | false]加载所有嵌入图像(默认值为true).也接受:[是|否].
在watir ::浏览器的初始化时,可以在:args键中指定这些设置为数组.例如:
args = %w{--load-images=false} browser = Watir::Browser.new(:phantomjs, :args => args)
一个工作示例:
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')
为屏幕截图带有加载的图像:
和未加载图像的屏幕截图:
问题描述
I am using Ruby and am using the latest gem version of Watir. I plan to use headless browser such as PhantomJS. How can one pass paramater to the Phantom JS browser when it get executed.
My purpose is so that Phantom JS do not need to load images.
推荐答案
As described on the PhantomJS command line page, there is an option to control the loading of images:
--load-images=[true|false] load all inlined images (default is true). Also accepted: [yes|no].
During the initialization of a Watir::Browser, these settings can be specified, as an array, in the :args key. For example:
args = %w{--load-images=false} browser = Watir::Browser.new(:phantomjs, :args => args)
A working example:
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')
Which gives the screenshot with images loaded:
And the screenshot without images loaded:
Notice that the Google image of the page is not loaded in the second screenshot (as expected since load-images was set to false).