Watir Changing Mozilla Firefox Preferences
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
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.
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/
精彩评论