【问题标题】:How to pass browser parameter to Watir如何将浏览器参数传递给 Watir
【发布时间】:2014-08-06 11:04:05
【问题描述】:

我正在使用 Ruby,并且正在使用 Watir 的最新 gem 版本。我打算使用无头浏览器,例如 PhantomJS。执行时如何将参数传递给 Phantom JS 浏览器。

我的目的是让 Phantom JS 不需要加载图片。

【问题讨论】:

    标签: ruby phantomjs watir


    【解决方案1】:

    PhantomJS command line page 所述,有一个选项可以控制图像的加载:

    --load-images=[true|false] 加载所有内联图片(默认为true)。也接受:[yes|no]。

    在 Watir::Browser 的初始化过程中,可以在 :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')
    

    这给出了加载图像的屏幕截图:

    还有没有加载图片的截图:

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

    【讨论】:

    • 似乎不再起作用了。图像仍在加载,其他命令行参数也不起作用。当我直接运行 phantomjs 作为带有 args 的测试时,它可以工作,但在 watir/ruby 中它只是没有任何区别。几个小时以来一直在尝试和谷歌搜索,现在找不到这是如何完成的......
    • @confetti,Selenium 弃用了他们对 phantomJS 的支持,因为 phantomJS 本身不再受支持。为了让它工作,我希望你需要确保你运行的是旧版本的 Selenium/Watir。虽然我会建议寻找其他浏览器 - 例如 Chrome 无头浏览器。
    • 是的,我确实有旧版本,我的 Phantom 工作正常。我还没有尝试其他浏览器,也许是时候尝试了。但永远不要改变正在运行的系统。 ? 这个:stackoverflow.com/a/43969985/9138821 答案实际上非常有效! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    相关资源
    最近更新 更多