Watir Changing Mozilla Firefox Preferences

纵饮孤独 提交于 2019-12-05 01:23:09

问题


I'm running a Ruby script using Watir to automate some things for me. I'm attempting to automatically save some files to a certain directory. So, in my Mozilla settings I set my default download directory to say the desktop and choose to automatically save files.

These changes, however, are not reflected when I begin to run my script. It seems like the preferences revert back to default. I've included the following

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

and open a new firefox instance with:

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

Any ideas on why the preferences would be set back by this? Or any alternative ideas for what I am trying to do? (Automatically save files).

Thanks


回答1:


WebDriver uses a clean profile for each browser instance, which is why the preferences appear to be "reset". You can tell it to use your default profile:

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

or tweak profile preferences programatically before launching the browser:

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

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

For an example of configuring automatic file downloads, see this section on the Selenium wiki.




回答2:


change default Watir preferences for download location

for chrome

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

for firefox

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

note: to be be able to access the Rails.root/lib folder easily from within your rails app, you'll need to add this code or something like it to your config/application.rb file:

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

for more information: http://watirwebdriver.com/browser-downloads/



来源:https://stackoverflow.com/questions/6564392/watir-changing-mozilla-firefox-preferences

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