0

With selenium I am trying to download a zip file, but the download window keeps popping up despite the many suggestions in this question. They do not work for unknown reasons. Maybe because the download-link is inside an iframe?

Anyway, I need to access the download popup with selenium to click on the Save to disk button and the OK button, OR to right-click on the download link to select the option Save link as ...

I cannot post a working example, as the webpage in question is not public. Maybe a class definition interfers with the profile settings etc etc.

So is there a way to access the popup dialog of the Download dialog?

Related question: here

For completeness: Here are all the profile settings:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir", download_dir)
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.useDownloadDir", True);
profile.set_preference("browser.download.manager.showWhenStarting", False )
profile.set_preference("pdfjs.disabled", True )
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream")
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);  
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting",False);
Alex
  • 41,580
  • 88
  • 260
  • 469
  • You need to set the mime returned by the request for the downloaded file in the preference `browser.helperApps.neverAsk.saveToDisk`. – Florent B. Sep 18 '17 at 09:30
  • I did. Please check the complete profile I provided in my question – Alex Sep 18 '17 at 09:35
  • So what is the exact MIME returned in the header of the response? It not clear since the preference contains many MIME unrelated to your question. – Florent B. Sep 18 '17 at 09:45
  • It is a zip file. I have updated the question – Alex Sep 18 '17 at 10:56
  • A zip file can have multiple MIME. You need to use the one returned in the response. See https://stackoverflow.com/questions/36309314/set-firefox-profile-to-download-files-automatically-using-selenium-and-java/36309735#36309735 – Florent B. Sep 18 '17 at 11:04
  • Aah, interesting, did not know that! The content-type seems to be `applicationforce-download/`... And that is the solution! Please create a full answer so I can give you the credits... And thanks a lot! – Alex Sep 18 '17 at 11:09

1 Answers1

0

In case the configuration does not work, I used an autoIT script to do the job:

$result = 1
$txt = ""
WinWait($CmdLine[2])
WinActivate($CmdLine[2],"")
WinFlash($CmdLine[2], "", 1, 50)
If (StringCompare("Opening", $CmdLine[2], 0) = 0) Then ;For FF
   Send("!s");
   Send("{ENTER}")
   WinWait("Enter name of file to save to…")
   WinActivate("Enter name of file to save to…","")
   WinFlash("Enter name of file to save to…", "", 1, 50)
   $txt = ControlGetText("Enter name of file to save to…", "", "[CLASS:Edit; INSTANCE:1]")
   If (StringCompare($CmdLine[1], $txt, 0) = 0) Then
      $result = 0
   EndIf
   ControlClick("Enter name of file to save to…", "", "[CLASS:Button; INSTANCE:2]")
   WinWaitNotActive("Enter name of file to save to…")
EndIf

If (StringCompare("Save As", $CmdLine[2], 0) = 0) Then ; For Chrome
   $txt = ControlGetText($CmdLine[2], "", "[CLASS:Edit; INSTANCE:1]")
   If (StringCompare($CmdLine[1], $txt, 0) = 0) Then
      $result = 0
   EndIf
   ControlClick($CmdLine[2], "", "[CLASS:Button; INSTANCE:2]")
   WinWaitNotActive($CmdLine[2])
EndIf
Exit $txt

Or following autoIt script:

    #include <file.au3>

_Log("----new session----")

if $CmdLine[0] <> 3 then
; Arguments are not enough
 msgbox(0,"Error","Supply all the Arguments, Browser name and path to upload")
_Log("Arguments are wrong")
Exit
EndIf

;Activate firefox/IE browser
If (StringCompare($CmdLine[1],"firefox",0) = 0) Then
    $waitTime=$CmdLine[3]
    if (WinWait("[Class:MozillaDialogClass]","",$waitTime)==0) Then
        _Log("Window Not Found From FireFox")
    Else
    _Log("Waiting For Download Window From FireFox")
    _FFDownload()
    EndIf

ElseIf (StringCompare($CmdLine[1],"ie",0) = 0) Then
    WinWait("File Download - Security Warning")
    _Log("Waiting For Download Window From IE")
    _IEDownload()
Else
    Exit
EndIf
;Used For IE and Tested on IE6
Func _IEDownload()

    $hResult = WinActivate("File Download - Security Warning")
    If($hResult == 0) Then
        _Log("Unable to find Download Window from IE")
    Else
        $IETitle=WinGetTitle("File Download - Security Warning")
        _Log("Download Window activated"&$IETitle)
        WinActivate($IETitle)
        ControlClick($IETitle, "","[CLASS:Button; INSTANCE:2]")
        _Log("FileChooser Window opend")
        _Log("CommandLine Parameter Found and Value is:"&$CmdLine[2])
        WinActivate("Save As")
        _Log("FileChooser Window opend"&WinGetTitle("Save As"))
        ControlSetText(WinGetTitle("Save As"),"","Edit1",$CmdLine[2])
        Send("!s")
    EndIf
EndFunc

;Used for FireFox Browser
Func _FFDownload()
    $hResult = WinActivate("[Class:MozillaDialogClass]");

    If($hResult == 0) Then
        _Log("Unable to find Download Window From FireFox")
    Else
        ; If firefox is set the save the file on some specif location without asking to user.
        ; It will be save after this point.
        ;If not A new Dialog will appear prompting for Path to save
        _Log("Download Window activated")
        ;To change the focus on save button
        Send("{TAB}")
        Sleep(400)
        Send("{TAB}")
        _Log("Change Focus to Save Button")
        ;This is use for if dialoguebox have save and openwith option
        Send("!s")
        ;Click on Enter to Save
        Sleep(400)
        Send("{ENTER}")
        _Log("Press Enter")
        Sleep(400)

        If(WinExists(WinGetTitle("[ACTIVE]"))) Then
            WinActivate("Enter name of file to save to…")
            $title = WinGetTitle("[ACTIVE]")
            if($title=="Enter name of file to save to…")Then
            _Log("Active Window Title Is:"&WinGetTitle("[ACTIVE]") )
            _Log("EnterName Window Opened And Tiltle is:"&$title)
            ;ControlSetText($title,"","Edit1",$CmdLine[2])
            ControlFocus($title, "", "Edit1")
            Send($CmdLine[2])
            Sleep(3000)
            ; Save File
            _Log("CommandLine Parameter Found For FilePath and Value is:"&$CmdLine[2])
            ;Send("!s")
            ;Sleep(3000)
            ControlClick("[CLASS:#32770]", "", "Button1")
            EndIf
        EndIf

    EndIf
EndFunc
;used for logging
Func _Log($sLogMsg)
_FileWriteLog(@ScriptDir & "\AutoItLog.log",$sLogMsg)
EndFunc
Tony Bui
  • 815
  • 5
  • 7