【问题标题】:Watir Changing Mozilla Firefox PreferencesWatir 更改 Mozilla Firefox 首选项
【发布时间】:2011-09-27 17:00:07
【问题描述】:

我正在运行一个使用 Watir 的 Ruby 脚本来为我自动化一些事情。我正在尝试将一些文件自动保存到某个目录。因此,在我的 Mozilla 设置中,我将默认下载目录设置为桌面并选择自动保存文件。

但是,当我开始运行我的脚本时,这些更改并没有反映出来。似乎偏好恢复为默认值。我已经包括以下内容

require "rubygems"         # Optional.
require "watir-webdriver"  # For web automation.
require "win32ole"         # For file save dialog.

并使用以下命令打开一个新的 firefox 实例:

browser = Watir::Browser.new(:firefox)

关于为什么偏好会因此而受挫的任何想法?或者我正在尝试做的任何替代想法? (自动保存文件)。

谢谢

【问题讨论】:

    标签: ruby scripting automation screen-scraping watir


    【解决方案1】:

    更改下载位置的默认 Watir 首选项

    镀铬

    profile = Selenium::WebDriver::Chrome::Profile.new
    download_dir = File.join(Rails.root, 'lib', 'assets')
    profile['download.default_directory'] = download_dir
    profile[download.prompt_for_download] = false
    @b = Watir::Browser.new :chrome, :profile => profile
    

    火狐

    profile = Selenium::WebDriver::Firefox::Profile.new    
    download_dir = File.join(Rails.root, 'lib', 'assets')
    profile['browser.download.dir'] = download_dir
    profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
    @b = Watir::Browser.new. :firefox, :profile => profile
    

    注意:为了能够从您的 rails 应用程序中轻松访问 Rails.root/lib 文件夹,您需要将此代码或类似代码添加到您的 config/application.rb 文件中:

    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    

    更多信息:http://watirwebdriver.com/browser-downloads/

    【讨论】:

      【解决方案2】:

      WebDriver 为每个浏览器实例使用一个干净的配置文件,这就是首选项似乎被“重置”的原因。您可以告诉它使用您的默认配置文件:

      Watir::Browser.new :firefox, :profile => "default" 
      

      或在启动浏览器之前以编程方式调整配置文件首选项:

      profile = Selenium::WebDriver::Firefox::Profile.new
      profile['some.preference'] = true
      profile.add_extension "/path/to/some/extension.xpi"
      
      Watir::Browser.new :firefox, :profile => profile
      

      有关配置自动文件下载的示例,请参阅 Selenium wiki 上的 this section

      【讨论】:

      • 我用 profile['dom.disable_open_during_load'] = true 进行了尝试,当浏览器实际启动时,该值仍然为 false。
      猜你喜欢
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      相关资源
      最近更新 更多