Selenium Firefox Download PDF


Download online PDF file using Firefox. 


By default, Firefox will open the online PDF in the built-in PDF viewer.


The following C# code will create a new Firefox profile and use it with selenium.

   1: FirefoxProfile profile = new FirefoxProfile();


   3: // custom folder 

   4: profile.SetPreference("", 2); 


   6: // download folder location

   7: profile.SetPreference("", "C:\\XXXX");


   9: // for PDF, save to disk without asking

  10: profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");


  12: // disable Firefox's built-in PDF viewer 

  13: profile.SetPreference("pdfjs.disabled", true); // disable Firefox's built-in PDF viewer 


  15: var browserDriver = new FirefoxDriver(profile);


  17: // save an online PDF 

  18: browserDriver.Navigate().GoToUrl(;

Useful tip:

You can type “about:config” in Firefox browser address and create/search/view all these preference settings.

Other Approaches:

You can manually configure via Firefox’s Option –> Application.  Then you locate and load the profile from local disk..



Gmail IMAP Authentication Error Web Login Required

Gmail wants to promote the use of OAuth2 over simple username/password authentication.

If you use the password authentication, you may get an error saying “Web Login Required”.

If you don’t want to switch to OAuth2, then you need to go to your Google account’s setting (not Gmail setting).  Under “Sign-In & security”, look for “Allow less secure apps” and turn it to “ON”.


Selenium Checkbox StaleElementReferenceException

I have a function that sets a checkbox.  It involves a couple of steps:

  1. var cb = FindElement(locator)
  2. if (cb.Selected != newValue)
  3. cb.Click();
  4. verify cb.Selected equals newValue.

I find that if I do all 4 steps within one method, they work fine. 


I tried to code refactor.  So I created method A with step1 in it and it calls method B( step 2 & 3 & 4).  However, I start getting StaleElementException at Step 4.


1. A lot of people use try/catch to retry and it works.

2. In my specific, I find that if I have step 1 & 2 within one method, it is OK.


I don’t know the cause of the problem.  I guess this has to do with web page using modal dialog.  Click() seems to screw up the element.  It seems like one must call .Selected() right after it FindElement().