How to pass browser parameter to Watir

蓝咒 提交于 2019-12-21 19:47:11

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/25151204/how-to-pass-browser-parameter-to-watir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!