Automation · Blogroll · firefox · QA · selenium · Selenium-webdriver · watir-webdriver

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 🙂

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

  1. Hi,
    I have a link.when I clicked the link it will open new dialog box. I want to click save button on dialog box or it will auto save.
    Could you please let me know How to implement it in my code.

    my code is :

    require ‘selenium-webdriver’
    browser = Selenium::WebDriver.for :firefox
    browser.get “http://xxxx/yyyyy”
    browser.find_element(:xpath,’/html/body/a’).click;

    this will open new dialog box. where i need to implement ur code .

    Thanks in advance…

    1. Suresh,

      The dialog is different for FF and Chrome. Thats why we are creating profile with Auto download option,

      browser = Selenium::WebDriver.for :firefox

      Instead of this try the above code

      Sorry for the delayed reply.

        1. Alex, Oh!! If the above script is not working for you then we need to find the alternate way to complete this. If any help required pls call me on +91 9941414834. Sorry for the post and inconvenience.

  2. Hi,
    i want to download a pdf file and have writtten the bellow code . But its not working. Pls help
    public class BookDownload {

    public static void main(String[] args)
    {
    FirefoxProfile profile=new FirefoxProfile();
    profile.setPreference(“browser.download.folderList”,0);
    profile.setPreference(“plugin.disable_full_page_plugin_for_types”, “application/pdf,application/vnd.adobe.xfdf,application/vnd.fdf,application/vnd.adobe.xdp+xml”);
    profile.setPreference(“browser.helperApps.neverAsk.saveToDisk”,”application/pdf”);
    WebDriver driver=new FirefoxDriver(profile);
    driver.get(“http://it-ebooks.info/book/3123/”);
    WebDriverWait wait=new WebDriverWait(driver,20);
    wait.until(ExpectedConditions.elementToBeClickable(By.linkText(“Instant Selenium Testing Tools Starter”)));
    driver.findElement(By.linkText(“Instant Selenium Testing Tools Starter”)).click();

    }

    }

Leave a comment