3

I am making a programme to automatically download the data using selenium webdriver in python. When i click on "download" button following popup occours

.enter image description here

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/xlsx")

But it is not working in my case. Then I tried to switch to this window from main window by using following code

    parent_h = driver.current_window_handle
    handles = driver.window_handles
    handles.remove(parent_h)
    driver.switch_to_window(handles.pop())    

But now I am not getting how to interact with this window?

2 Answers 2

3

You should try to use preference with correct MIME type for xlsx extension which is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", but not "application/xlsx":

profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

You can check list of MIME types for Microsoft Office files here

Sign up to request clarification or add additional context in comments.

1 Comment

After using this mime also same pop-up window is appearing. Can you tell me another solution.
3

After so much findings and research, I get the following code which would be helpful for this type of situation.

    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);

1 Comment

after trying various SO solutions, i tried pdfjs.disabled as you mention, and it worked. however for me it only works in conjunction with browser.helperApps.neverAsk.saveToDisk, and the other settings are not needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.