4

I am writing a program to automate web interaction through selenium webdriver in python. I got stuck in last step when I click on the "download" button through script, a window pop-up occours on the screen,with default option "Open with" selected. I want my program to first click on the option "save file" and then click on "OK". I have used following piece of code to set up Firefox profile

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

One of my observation is that when the window popup is like this

enter image description here

with option "Do this automatically for files like this from now on" is clickable (via checkbox) then the above piece of code works perfect, but when the same option is not clickable (as shown in the image below) then above code for setting profile fails. Can anyone help me in this situation?

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user8023364
  • 159
  • 1
  • 12

1 Answers1

7

While you work with a new FirefoxProfile, use the set_preference method to configure the profile in such a way so clicks on Save and Ok and it doesn't gets interrupted in the downloading process. You can set the configuration as follows:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir",os.getcwd());
profile.set_preference("browser.download.folderList",2);
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.msexcel,application/vnd.ms-excel,text/anytext,text/comma-separated-values,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream");
profile.set_preference("browser.download.manager.showWhenStarting",False);
profile.set_preference("browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.msexcel,application/vnd.ms-excel,text/anytext,text/comma-separated-values,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream");
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("pdfjs.disabled", True);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thanx for answering, but unfortunately again the same pop-up as (in the 2nd image) is occouring. Am I required to do some changes in it or do you have any other solution – user8023364 Jun 03 '17 at 12:01
  • yeah sure take your tym. – user8023364 Jun 03 '17 at 12:08
  • Again the same popup is occouring. I am not getting why is this happening. As per your knowledge do this have a relation with option "Do this....." which is not clickable, the same which I mentioned in my question – user8023364 Jun 03 '17 at 12:36
  • 1
    Yeah dude now it works... Thanks a lot man @DebanjanB. Can you please provide me the source from where I can learn this profile setting step... – user8023364 Jun 03 '17 at 12:56
  • @user8023364 Glad to be able to help you :) the more you help others the more you learn through Research & Development. Thanks – undetected Selenium Jun 03 '17 at 12:59