Selenium Firefox Download PDF

Goal:

Download online PDF file using Firefox. 

Problem:

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

Solution:

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

   1: FirefoxProfile profile = new FirefoxProfile();

   2:  

   3: // custom folder 

   4: profile.SetPreference("browser.download.folderList", 2); 

   5:  

   6: // download folder location

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

   8:  

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

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

  11:  

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

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

  14:  

  15: var browserDriver = new FirefoxDriver(profile);

  16:  

  17: // save an online PDF 

  18: browserDriver.Navigate().GoToUrl(http://static.mozilla.com/moco/en-US/pdf/mozilla_privacypolicy.pdf);

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..

Reference:

http://yizeng.me/2014/05/23/download-pdf-files-automatically-in-firefox-using-selenium-webdriver/

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”.

image

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. 

Problem

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.

Solution

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.

Speculation

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().

 

References:

http://stackoverflow.com/questions/4846454/selenium-webdriver-staleelementreferenceexception

http://stackoverflow.com/questions/31379812/selenium-staleelementreferenceexception-explicit-wait

http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp