【问题标题】:How to access the download dialog with selenium in Firefox?如何在 Firefox 中使用 selenium 访问下载对话框?
【发布时间】:2017-09-18 20:00:42
【问题描述】:

使用selenium,我正在尝试下载zip 文件,但尽管many suggestions in this question,下载窗口仍不断弹出。他们不工作的原因不明。可能是因为下载链接在iframe 内?

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

我无法发布工作示例,因为相关网页未公开。也许类定义会干扰配置文件设置等。

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

相关问题:here

为了完整起见:以下是所有配置文件设置:

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

【问题讨论】:

  • 您需要在首选项browser.helperApps.neverAsk.saveToDisk中设置下载文件请求返回的mime。
  • 我做到了。请检查我在问题中提供的完整个人资料
  • 那么,响应标头中返回的确切 MIME 是什么?不清楚,因为首选项包含许多与您的问题无关的 MIME。
  • 这是一个压缩文件。我已经更新了问题
  • 一个 zip 文件可以有多个 MIME。您需要使用响应中返回的那个。见stackoverflow.com/questions/36309314/…

标签: python selenium firefox


【解决方案1】:

如果配置不起作用,我使用了一个 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

【讨论】:

    猜你喜欢
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多