Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically download files from a pop up dialog using selenium-python

I am trying to automatically download files from a pop up dialog using selenium-python.

The firefox popups look like this

enter image description here

I want to simulate clicking "OK"

I found this answer How do I trap a popup in Selenium 2 python which sent me to the docs https://selenium-python.readthedocs.org/en/latest/navigating.html?highlight=popup#popup-dialogs

I've tried this

    alert = driver.switch_to_alert()
    #alert.send_keys(Keys.RETURN) #No alert is present

and this

    alert = driver.switch_to_alert()
    alert.accept()  #no alert is present

If I run pprint.pprint(driver.window_handles) it prints only a single GUID -- showing that only one window is present.

So if no alert is present and there is only one window -- how do I download these files?

like image 740
bernie2436 Avatar asked Jul 20 '14 16:07

bernie2436


People also ask

Can Selenium download files?

In Selenium testing, it is very important to know how to Upload files in Selenium WebDriver or download files in Selenium WebDriver through automation testing with Selenium. In this Selenium Java tutorial, I am going to highlight different ways through which you can download or upload files in Selenium WebDriver.


7 Answers

In python, but this will work in Java as well because the firefox preferences are a javascript:

profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "c:\\firefox_downloads\\")
browser = webdriver.WebDriver(firefox_profile=profile)

this works for CSV files, modify it for whatever filetype you are downloading.

like image 78
Max Avatar answered Oct 06 '22 21:10

Max


Based on Amey's answer 1) and of course Yi Zeng's blog (in ruby) quoting Selenium itself doesn’t interact with system-level dialogs like this as well as the documentation, here is the python snippet to resolve the issue

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile = FirefoxProfile()
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/pdf')
driver = webdriver.Firefox(firefox_profile=profile)

driver.get(target_url)
#specific to target_url
driver.find_element_by_css_selector('a[title="Click to Download"]').click()
like image 44
SYK Avatar answered Oct 06 '22 22:10

SYK


I've discovered a useful solution that hopefully will help somebody out there.

You can skip the prompt altogether with firefox if you're starting the download with a click by holding the ALT key.

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
...
profile.set_preference("browser.altClickSave", True)
...
element = driver.find_element_by_xpath("//a")
ActionChains(driver).key_down(Keys.ALT).click(element).key_up(Keys.ALT).perform()

This should download any file without needing to specify MIME type of anything.

like image 43
Zei Avatar answered Oct 06 '22 22:10

Zei


I spent some time today trying to figure this out (again... had the solution at home but couldn't get to it...) and during that I found this... None of the solutions helped me so I thought I'd offer up what I did to solve this problem.

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

dl_path = "/tmp/"
profile = FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", false)
profile.set_preference("browser.download.dir", dl_path)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                          "text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
like image 40
Matt Camp Avatar answered Oct 06 '22 22:10

Matt Camp


You have two options :

1) Create a custom firefox profile with settings where the download location is pre-decided and firefox does not ask for confirmation to download. Just googled and found a blog that explains how to do it

2) Use sikuli to automate clicks on the download dialog box. Blog explaining- How to use Sikuli

P.S. - Not read the blogs, but I am sure they will give u a clue.

like image 35
Amey Avatar answered Oct 06 '22 21:10

Amey


With my using and test in my Selenium UI automation test, configuring the Firefox Profile is more stable than Robot Class. E.g. Disable popping up the System non-webpage Download/Save Dialog.

FirefoxProfile prof = new FirefoxProfile();

ffprofile.setPreference("browser.download.panel.shown", false);
ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");

//ffprofile.setPreference("browser.download.folderList", 1);  // Default to /home/user/Downloads in Linux.
ffprofile.setPreference("browser.download.folderList", 2); 
ffprofile.setPreference("browser.download.dir", "/tmp");
like image 35
Yang Avatar answered Oct 06 '22 20:10

Yang


FirefoxProfile fxProfile = new FirefoxProfile();
 fxProfile.SetPreference("browser.download.panel.shown", false);
 fxProfile.SetPreference("browser.helperApps.neverAsk.openFile", "text/csv,application/vnd.ms-excel");
 fxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/vnd.ms-excel");
 fxProfile.SetPreference("browser.download.folderList", 2); 
 fxProfile.SetPreference("browser.download.dir", "c:\\mydownloads");
 IwebDriver driver = new FirefoxDriver(fxProfile);
like image 43
Sachit Dahal Avatar answered Oct 06 '22 20:10

Sachit Dahal