How to download any file using selenium webdriver.what is the logic to download any file in selenium webdriver
-
possible duplicate of [Access to file download dialog in Firefox](http://stackoverflow.com/questions/1176348/access-to-file-download-dialog-in-firefox) – Edu Aug 02 '13 at 06:41
-
check this http://stackoverflow.com/q/16746707/624003 – Sankumarsingh Aug 02 '13 at 08:54
-
Possible duplicate of [How to download any file and save it to the desired location using Selenium Webdriver](http://stackoverflow.com/questions/16746707/how-to-download-any-file-and-save-it-to-the-desired-location-using-selenium-webd) – Liam Feb 23 '17 at 12:08
2 Answers
If you mean "any" file that browser would show, i.e. any html file, you just need to call
driver.getPageSource();
If you mean "any" file as in the "save link as" or similar menu of browsers you are out of luck, since this triggers the OS-driven download file chooser of a browser which can't be controlled by Selenium webdriver.
Solution to 2.
You have two options here I think:
a) use something like AutoIT ontop of selenium. This becomes very hard to control in a short time, is not portable and will make your tests error prone.
b) The better solution is probably to download the file outside of selenium. I found a nice article describing the whole dilemma here. It also contains a nice solution to the problem that takes even care of cookie-handling if needed for the download.

- 11,497
- 6
- 38
- 53
Actually it's not a good idea to download files using web-driver. I don't think that you need the file; in most cases, it's just for testing download links and for this purpose you can use driver.find_element_by_tag_name("a")
, driver.find_elements_by_link_text("some text")
or driver.find_elements_by_partial_link_text("a")
; after finding the element, you just click()
on it and check if the response's URL is actually what it should be.
Still if you have a goal for completely downloading the file, I'd be glad if you let me know.
EDIT:
Today, I visit this page and I think this will help you, specially this comment.

- 9,835
- 23
- 82
- 131
-
It somehow depends on the page you are trying to download the file from. If you have the complete download URL and you are not insisted to use web-driver, you can use `urllib` library as this: `urllib.urlretrieve("download_url")`. I tried this command for both internet and intranet file downloads; both were successful, but it seems that in some cases for intranet files it can't correctly understand the file extension. – Zeinab Abbasimazar Feb 25 '14 at 06:49
-
Yeah, I have to use selenium for this unfortunately. Wish I could use urllib in this case. – User Feb 25 '14 at 15:17
-
As the link I have provided in the answer describes, you can do it using Robot or AutoIt; but without them you should get the page where the link is located there, find it with the driver and finally click on it. It is all that comes into my mind by now. – Zeinab Abbasimazar Feb 25 '14 at 16:03