0 votes
1 view
in Devops and Agile by (25k points)

The old methods of downloading a file via Selenium no longer seem to work.

My code is:

    fp = webdriver.FirefoxProfile()

    fp.set_preference("browser.download.dir", os.getcwd())

    fp.set_preference("browser.download.folderList", 2)

    fp.set_preference("browser.download.manager.showWhenStarting", False)

    fp.set_preference("browser.helperApps.neverAsk.saveToDisk",

                      "application/pdf")

    self.driver = webdriver.Firefox(firefox_profile=fp)

    self.longMessage = True

However, the file dialog still appears. I've done quite a bit of toggling fields on and off, but after a bit of digging i found that there are no differences between the prefs.js file of a default Firefox profile generated by Selenium and the prefs.js file of one where i have manually checked "Do this automatically for files of this type from now on" on the download dialog.

The mimeTypes.rdf file does change, though—specifically, the lines below are added:

<RDF:Description RDF:about="urn:mimetype:handler:application/pdf"

               NC:alwaysAsk="false"

               NC:saveToDisk="true"

               NC:handleInternal="false">

<NC:externalApplication RDF:resource="urn:mimetype:externalApplication:application/pdf"/>

I don't know of a way to set up a custom mimeTypes.rdf file when creating a new Firefox Profile, though. Does anyone have any idea?

To pre-empt anyone suggesting that i just cURL the download URL, the file is generated for the user and i need to specifically verify that a .pdf file is downloaded to the drive.

1 Answer

0 votes
by (63.3k points)

I'm an R user so just posting my solution in R using RSelenium. If you are not able to convert the same in python kindly let me know I'll hint upon the same.

 

known_formats <- c("application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

firefox_profile.me <- makeFirefoxProfile(list(marionette = TRUE,                                           #this is for certificate issues [may be ignored]

                                              webdriver_accept_untrusted_certs = TRUE,

                                            webdriver_assume_untrusted_issuer = TRUE,                                             

#download related settings

                                            browser.download.folderList = 2L,

                                          browser.download.manager.showWhenStarting = FALSE,

                                            # put your path here but remember to give path like C:\\DirOfYourChoice and not C:\\DirOfYourChoice\\ [last \\ is not going to work]

                                          browser.download.dir = normalizePath("TestDL"),                                             browser.helperApps.alwaysAsk.force = FALSE,

                                            browser.helperApps.neverAsk.openFile = paste0(known_formats, collapse = ","),

                                          browser.helperApps.neverAsk.saveToDisk = paste0(known_formats, collapse = ","),

                                        browser.download.manager.showWhenStarting = FALSE,

#this is for marionette and related security                                          "browser.tabs.remote.force-enable" = TRUE, pdfjs.disabled = TRUE))

remDr <- remoteDriver(remoteServerAddr = "localhost",

                      port = 4444,

                      browserName = "firefox",

                      extraCapabilities = firefox_profile.me)

remDr$open()

remDr$navigate("https://www.google.com/search?q=sample+xlsx")

remDr$findElement(using = "css selector", value = ".g:nth-child(1) a")$clickElement()

remDr$navigate("https://www.google.com/search?q=test+xls")

remDr$findElement(using = "css selector", value = ".g:nth-child(1) a")$clickElement()

Working fine with me I'm using

 

Firefox 50.1.0 [while I'm writing this post]

Selenium [3.0.1]

R [3.3.2 (2016-10-31)]

I hope you will be able to port this to python. Just try to replicate the configurations in firefox makeFirefoxProfile

 

Reference for further understanding:-

How to Download files using Selenium

Firefox Profile Settings in Selenium

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...