Watir / Selenium Web Driver – Browser Downloads — Solution to download pdf files automatically in firefox 20+ version

 

Solution to download pdf files automatically in firefox 20+ version using Selenium webdriver or Watir webdriver

 

Description:

I have tried to save PDF files automatically using the below link — http://watirwebdriver.com/browser-downloads/  and I have followed the same as per the mentioned page and reference website. But still the firefox browser not downloaded PDF files automatically.

Reason:

In Firefox latest versions, They have added new function called as ‘Portable Document Format’ which causes the issue

Solution:

Please try the below code for latest firefox versions.

download_directory = "#{Dir.pwd}/downloads"

download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"

profile[‘pdfjs.disabled’] = true
profile[‘pdfjs.firstRun’] = false

b = Watir::Browser.new :firefox, :profile => profile
Note:
profile[‘pdfjs.disabled’] = true
profile[‘pdfjs.firstRun’] = false
These 2 lines are added. Thats it 🙂

Watir WebDriver — Handling New Window

Situation:

1. You are in Parent Page.
2. Clicking link “Open” in Parent page.
3. It opens new window
4. You need to do actions  there and come back to your parent window

Solution:

To Click the Link

require 'rubygems'
require 'watir-webdriver'
ff=Watir::Browser.new :ff
ff.goto("website.com")
ff.link(:text,'open').click

To handle the New window and performing some actions within that window,

ff.window(:title,/TITLE of the new window/i).use do
ff.send_keys('SampleText')
ff.button(:id,'insert').click
puts ff.title #returns the new window title
end

within a loop The button belongs to Newly opened window.

puts ff.title #returns the parent window title