如何在Firefox中使用selenium访问下载对话框?

时间:2017-09-18 09:15:38

标签: python selenium firefox

使用selenium我正在尝试下载zip文件,但是尽管many suggestions in this question,下载窗口仍然会弹出。他们不是因为不明原因而工作。也许是因为下载链接在iframe

无论如何,我需要使用selenium访问下载弹出窗口,点击Save to disk按钮和OK按钮,或者右键单击下载链接以选择{{1}选项}

我无法发布一个有效的示例,因为相关网页不公开。也许类定义会干扰配置文件设置等等。

那么有没有办法访问“下载”对话框的弹出对话框?

相关问题:here

完整性:以下是所有配置文件设置:

Save link as ...

1 个答案:

答案 0 :(得分:0)

如果配置不起作用,我使用autoIT脚本来完成这项工作:

$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

或者关注autoIt脚本:

    #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