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.
I'm a 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)]
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
you can create additional method for download files from the internet by link.
Example frommy c# code:
public Bitmap Image
{
get
{
string webPath = Element.GetAttribute("src");
if (webPath != string.Empty)
{
try
{
System.Net.WebRequest request =
System.Net.WebRequest.Create(webPath);
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
Bitmap bitmapImg = new Bitmap(responseStream);
return bitmapImg;
}
catch (System.Net.WebException)
{
}
}
return new Bitmap(1,1);
}
}
so as you see, in this code I get src attribute from image element and download it extarnally from browser to get absolutely correct bitmap image (after this I can save it to HDD). By the same way you can download any files from links =)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.