2016-10-08 4 views
1

Meine Anforderung ist es, Datei von Internet Explorer (Version 11) herunterladen und speichern Sie bestimmte Speicherort mit Selenium und AutoIT. Der Pfad zum Speichern der Datei wird über die Befehlszeile angegeben.AutoIT: Datei vom IE herunterladen und im genannten Verzeichnis speichern

Hier ist mein Code:

; get the handle of main window 
Local $windHandle= WinGetHandle("[Class:IEFrame]", "") 
Local $winTitle = "[HANDLE:" & $windHandle & "]" 

; Select save as option 
WinActivate ($winTitle, "") 
Send("{F6}") 
sleep(500) 
Send("{TAB}") 
sleep(500) 
Send("{DOWN}") 
sleep(500) 
Send("a") 

; Save as dialog 
; wait for Save As window 
WinWait("Save As") 
; activate Save As window 
WinActivate("Save As") 
ControlFocus("Save As","","Edit1") 
ControlSetText("Save As","","Edit1",$CmdLine[1]) 
Sleep(2000) 
ControlClick("Save As","","Button2") 

===Execution of above code through selenium===== 

Runtime.getRuntime().exec("D:\\AutoIT\\downloadFile.exe"+" "+"D:\\AutoIT\\abc.pdf"); 

Es funktioniert aber nicht statt in bestimmten Datei gespeichert bekommen es spart in Standardverzeichnis heißt je nachdem, was Standort Fenster zeigt „Speichern unter“.

Bitte helfen.

Antwort

0

Warum versuchen Sie, mit dem unsicheren Send() - Befehl zu arbeiten? AutoIt hat die Funktion INetGet() um Dateien herunterzuladen. Ich habe manchmal eine einfach zu bedienende Download-Funktion mit kleiner GUI gemacht, um Fortschritt, Geschwindigkeit und verbleibende Zeit anzuzeigen. Kann es nützlich für Sie sein:

#include <APIShellExConstants.au3> 
#include <GuiStatusBar.au3> 
#include <InetConstants.au3> 
#include <ProgressConstants.au3> 

; #FUNCTION# ==================================================================================================================== 
; Name ..........: _DownloadFile 
; Description ...: Downloads a file from a given URL, shows: progress, speed and remaining time. 
; Syntax ........: _DownloadFile($_sUrl, $_sFile[, $_sTitle = 'Download'[, $_iWidth = -1[, $_iHeight = -1[, $_iX = -1[, $_iY = -1]]]]]) 
; Parameters ....: $_sUrl    - The web adress of the file to load. 
; ...............: $_sFile    - The path to store this file. 
; ..[optional]...: $_sTitle    - Title of the download window. Default is '', = 'Download'. 
; ..[optional]...: $_iWidth    - Width of the download window. Default is -1, = 250. 
; ..[optional]...: $_iHeight   - Height of the download window. Default is -1, = 75. 
; ..[optional]...: $_iX     - X position of the download window. Default is -1, centered. Other negative value: X px from right, postive values: X px absolute. 
; ..[optional]...: $_iY     - Y position of the download window. Default is -1, centered. Other negative value: Y px from bottom, postive values: Y px absolute. 
; Return values .: Success    None 
; ...............: Failure    1 and show message box - download has failed or was escaped. 
; Author ........: BugFix 
; =============================================================================================================================== 
Func _DownloadFile($_sUrl, $_sFile, $_sTitle='', $_iWidth=-1, $_iHeight=-1, $_iX=-1, $_iY=-1) ; Pos: -1/-1 = center, -X/-Y = X px from right/Y px from bottom 
    $_sTitle = $_sTitle = '' ? 'Download' : $_sTitle 
    $_iWidth = $_iWidth = -1 ? 250 : $_iWidth 
    $_iHeight = $_iHeight = -1 ? 75 : $_iHeight 
    If FileExists($_sFile) Then FileMove($_sFile, $_sFile & '.bak', 1) 

    ; Download in background, wait until DL complete - show it in GUI 
    Local $iBytesSize, $hDL = GUICreate($_sTitle, $_iWidth, $_iHeight, -1, -1, BitOR(0x00C00000,0x00080000)) ; WS_CAPTION,WS_SYSMENU 
    Local $idDL_sum = GUICtrlCreateLabel('0,000 KB', 5, 5, $_iWidth-10, 30, 0x01) ; SS_CENTER 
    GUICtrlSetFont(-1, 14, Default, Default, 'Verdana') 
    Local $aParts[3] = [75,$_iWidth-75,-1] 
    Local $hStatus = _GUICtrlStatusBar_Create($hDL, $aParts) 
    _GUICtrlStatusBar_SetMinHeight($hStatus, 25) 
    Local $idProgress = GUICtrlCreateProgress(0, $_iHeight-20-26, $_iWidth, 20, $PBS_SMOOTH) 
    Local $iTimer = TimerInit() 
    ; get absolute window size, move window 
    Local $aWin = WinGetPos($hDL) 
    Select 
     Case $_iX = -1 
      $_iX = (@DesktopWidth - $aWin[2])/2 
     Case $_iX < -1 
      $_iX = @DesktopWidth - ($aWin[2] - $_iX) 
    EndSelect 
    Select 
     Case $_iY = -1 
      $_iY = (@DesktopHeight - $aWin[3])/2 
     Case $_iY < -1 
      $_iY = @DesktopHeight - ($aWin[3] - $_iY) 
    EndSelect 
    WinMove($hDL, '', $_iX, $_iY) 
    GUISetState() 

    Local $iSizeSource = InetGetSize($_sUrl) 
    Local $hDownload = InetGet($_sUrl, $_sFile, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND) 
    _GUICtrlStatusBar_SetText($hStatus, @TAB & _FormatByte($iSizeSource, '', False, 0), 2) 

    Local $fEsc = False, $iDL_old = 0, $iDL_diff, $iTimeNeed 
    Do 
     If TimerDiff($iTimer) >= 1000 Then 
      $iBytesSize = InetGetInfo($hDownload, $INET_DOWNLOADREAD) 
      GUICtrlSetData($idDL_sum, StringReplace(_FormatByte($iBytesSize), '.', ',')) 
      GUICtrlSetData($idProgress, Int(($iBytesSize/$iSizeSource)*100)) 
      $iDL_diff = $iBytesSize - $iDL_old 
      $iDL_old = $iBytesSize 
      _GUICtrlStatusBar_SetText($hStatus, @TAB & _FormatByte($iDL_diff, '', False, '0') & '/s', 0) 
      $iTimeNeed = ($iSizeSource - $iBytesSize)/$iDL_diff 
      _GUICtrlStatusBar_SetText($hStatus, @TAB & _FormatSeconds($iTimeNeed), 1) 
      $iTimer = TimerInit() 
     EndIf 
     If GUIGetMsg() = -3 Then 
      $fEsc = True 
      ExitLoop 
     EndIf 
    Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE) 
    _GUICtrlStatusBar_SetText($hStatus, '', 0) 
    _GUICtrlStatusBar_SetText($hStatus, '', 1) 

    ; get info about DL file 
    Local $aData = InetGetInfo($hDownload) 
    If @error Or $fEsc Then 
     FileDelete($_sFile) 
     Local $sTmp = $fEsc ? 'escaped' : 'failed' 
     GUICtrlSetData($idDL_sum, 'Download ' & $sTmp & '!') 
     Return SetError(1,0,MsgBox(262192, 'Error', 'The Download has ' & $sTmp & '.')) 
    Else 
     GUICtrlSetData($idDL_sum, StringReplace(_FormatByte($iBytesSize), '.', ',')) 
     GUICtrlSetData($idProgress, 100) 
     Sleep(500) 
     GUICtrlSetData($idDL_sum, 'Download finished.') 
    EndIf 
    InetClose($hDownload) 
    GUIDelete($hDL) 
EndFunc ;==>_DownloadFile 


; #FUNCTION# ==================================================================================================================== 
; Name ..........: _FormatSeconds 
; Description ...: Returns a given value of seconds in the format 
; ...............:  <24h: "hh:mm:ss", >=24h: "x d/hh:mm:ss h" 
; Syntax ........: _FormatSeconds($_sec) 
; Parameters ....: $_sec    - The number of seconds. 
; Return values .: The formatted string. 
; Author ........: BugFix 
; =============================================================================================================================== 
Func _FormatSeconds($_sec) 
    Return ($_sec < 60 ? StringFormat('00:00:%02u', $_sec) : _ 
      $_sec < 60*60 ? StringFormat('00:%02u', Floor($_sec/60)) & ':' & _ 
        StringFormat('%02u', Mod($_sec,60)) : _ 
      $_sec < 60*60*24 ? StringFormat('%02u', Floor($_sec/3600)) & ':' & _ 
        StringFormat('%02u', Floor(Mod($_sec,3600)/60)) & ':' & _ 
        StringFormat('%02u', Mod(Mod($_sec,3600),60)) : _ 
      ($_sec = 86400 ? "24:00:00" : Floor($_sec/86400) & ' d/' & _ 
        StringFormat('%02u', Floor(Mod($_sec,86400)/3600)) & ':' & _ 
        StringFormat('%02u', Floor(Mod(Mod($_sec,86400),3600)/60)) & ':' & _ 
        StringFormat('%02u', Mod(Mod(Mod($_sec,86400),3600),60)) & ' h')) 
EndFunc ;==>_FormatSeconds 

; #FUNCTION# ==================================================================================================================== 
; Name ..........: _FormatByte 
; Description ...: Formats a given value of bytes with highest or given unit, optional as structure with all units 
; Parameters ....: $_iByte The value of bytes to format 
; ...............: $_sUnit (Default = '', unit of highest value) or count of given unit (TB, GB, MB, KB, Byte) 
; ...............: $_fStruct Returns a structure with .TB .GB .MB .KB .Byte (Default = False) 
; ...............: $_sDigit Number of decimal digits (Default = '3') as string! 
; Return values .: The formatted string or the structure. 
; Author ........: BugFix 
; =============================================================================================================================== 
Func _FormatByte($_iByte, $_sUnit='', $_fStruct=False, $_sDigit='3') 
    Local Static $aByte[5][2] = [[0x10000000000],[0x40000000],[0x100000],[0x400],[0x1]] 
    Local Static $tBytes = DllStructCreate('int TB;int GB;int MB;int KB;int Byte;') 
    Local Static $aUnit[5] = ['TB','GB','MB','KB','Byte'] 
    Local $iModulo = $_iByte, $iHighest = 4 
    For $i = 0 To 3 
     $aByte[$i][1] = $iModulo >= $aByte[$i][0] ? Floor($iModulo/$aByte[$i][0]) : 0 
     $iModulo = $aByte[$i][1] > 0 ? Mod($iModulo,$aByte[$i][0]) : $iModulo 
     $iHighest = $aByte[$i][1] > 0 ? ($i < $iHighest ? $i : $iHighest) : $iHighest 
    Next 
    $aByte[4][1] = $iModulo 
    If $_fStruct Then 
     $tBytes.TB = $aByte[0][1] 
     $tBytes.GB = $aByte[1][1] 
     $tBytes.MB = $aByte[2][1] 
     $tBytes.KB = $aByte[3][1] 
     $tBytes.Byte = $aByte[4][1] 
     Return $tBytes 
    EndIf 
    $_sUnit = StringInStr('TB GB MB KB Byte', $_sUnit) ? $_sUnit : '' 
    $_sUnit = $_sUnit = '' ? $aUnit[$iHighest] : $_sUnit 
    Local $iUserUnit = Floor(StringInStr('TB GB MB KB Byte', $_sUnit)/3) 
    If Number($_sDigit) < 0 Then $_sDigit = '0' 
    Local $sFormat = '%.' & $_sDigit & 'f %s' 
    Return StringFormat($sFormat, $_iByte/$aByte[$iUserUnit][0], $aUnit[$iUserUnit]) 
EndFunc ;==>_FormatByte 
Verwandte Themen