问题
Capybara.register_driver :session do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
Capybara::Selenium::Driver.new app, :profile => profile
end
What do I add here to get my ruby scripts to tell Selenium (I am running on a MAC OS El Capitan) to interact with the Firefox Open/Save dialog box such that it opens a PDF or DOC file automatically instead of prompting me with the dialog box which requires manual intervention?
回答1:
You can't interact with the Open/Save dialog box using selenium-webdriver with firefox. However, you can set preferences in your driver to make the dialog box never appear and a default behavior to just happen. Something like
Capybara.register_driver :session do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = "~/Downloads"
# Adjust below to match the type of file reported
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf, application/octet-stream"
Capybara::Selenium::Driver.new app, :profile => profile
end
should cause it to always download the files without prompting. You can similarly set the 'browser.helperApps.neverAsk.openFile' profile setting to have specific types of files always be opened.
来源:https://stackoverflow.com/questions/42958611/ruby-interacting-with-selenium-to-control-firefox-open-save-dialog