Jump to content
Sign in to follow this  
jack_

Not able to toggle dialog box using selenium in Python

Recommended Posts

I am using "Python-3.4.1 32 bit" on "Windows 7 64 bit" machine with "Selenium" using "Firefox 33". I am trying to automate downloading a "CSV" file from a secured website after furnishing the necessary credentials. I have written the required code to navigate to the correct element to initiate the download of the file but I am stuck when Firefox gives the download box (Dialog box title: Opening export.csv, "Open with" and "Save File" options as radio buttons with "OK" and "Cancel" as buttons). I want the file to be downloaded automatically and for that I have tried the following codes without success:

from selenium import webdriverfp = webdriver.FirefoxProfile()fp.set_preference("browser.download.manager.showWhenStarting",False)fp.set_preference("browser.download.folderList",2)fp.set_preference("browser.download.dir", "C:UsersarunDownloads")fp.set_preference("browser.download.downloadDir", "C:UsersarunDownloads")fp.set_preference("browser.download.defaultFolder", "C:UsersarunDownloads")fp.set_preference('browser.helperApps.neverAsk.saveToDisk',"text/csv")driver = webdriver.Firefox(fp)driver.get("http://website-url.com")

and then click on the button to start the download. But, it still shows the dialog box. I even tried handling it as an alert and tried

alert = driver.switch_to_alert()alert.accept()

but this always gave an exception of alert not found which led me to the conclusion that it might not be a JavaScript functionality after all but rather a web element. I am giving sufficient delay for the alert box to appear before I try to interact with it using the "alert" object code shown above.What do I do to get this downloaded automatically without the dialog box interaction (the way it's handled in Google Chrome browsers)I think that the server is not assigning the correct MIME type for the file that is being downloaded? The file I am downloading is of type: "text/csv" and in Firefox when the dialog box appears, "Do this automatically for files like this from now on" check box is greyed out.Thanks for your help and timeCheers!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×