如何处理 print- 和 "Save Print output as" 浏览器 windows?

How to handle print- and "Save Print output as" browser windows?

  1. 我必须访问 URL,使用凭据登录,然后 select 区域名称并单击显示按钮,以便 [=68= 中的最近账单] 页面显示(所有这些都是我使用 Selenium 脚本完成的)。

  2. 那个页面上有一个按钮打印选项。单击后会出现打印弹出窗口,我需要单击 确定。但是我也无法使用 AutoIt 脚本这样做。

  3. 单击 确定 完成后,“将打印输出另存为”window 打开我必须在其中输入文件名并单击 保存

这两个弹出窗口 windows 与 Firefox 和 Chrome 不同。如何处理这些?我尝试使用 AutoIt 脚本并在 Selenium 脚本中使用 Runtime .exec(file name) 调用它,但这对我来说都不起作用。

WinWait("Print", "", 5000)

If WinExists("Print", "") Then
    Send("OK{ENTER}")
EndIf

Sleep(5000)
WinWait("Save Print Output As", "", 5000)

If WinExists("Save Print Output As", "") Then
    ControlFocus("Save Print Output As", "", "Edit1")
    Sleep(5000)
    ControlSetText("Save Print Output As", "", "Edit1", "H282")
    Sleep(5000)
    ControlClick("Save Print Output As", "", "Button2")
EndIf

我还需要针对不同地区多次 运行 脚本,但它在第一次 运行 后停止执行。

'Print' 和 'Save as Print Output' 弹出窗口的 Window 信息工具摘要是-

AutoIt Window Info of Print pop-up

AutoIt Window Info of 'Save as Print Output' for the Field to Enter File Name

AutoIt Window Info of 'Save as Print Output' for the Save Field

问题是 - selenium 代码执行良好,当单击打印选项然后处理打印 window,调用 .exe 文件并在后台开始 运行。但不起作用。一旦打印 window 打开,执行就会停止。

Now the New pop-up is seen , when file name is entered in 'Edit1" for each different file name

Opt("TrayIconDebug", True)

; String in filename to replace with an incremented integer.
$sTag = "++1"

; Show custom progress window (True|False).
$bEnableProgress = True

Switch $CMDLINE[0]
    Case 0
        Exit

    Case 1
        If $CMDLINE[1] = '/?' Then
            ; Help message.
            _HelpMsg()
            Exit
        Else
            ; Assign default command line array.
            $aFilenames = $CMDLINE
        EndIf

    Case 2
        ; If not $sTag in the 1st argument, then goto the next case.
        If Not StringInStr($CMDLINE[1], $sTag) Then ContinueCase

        ; If the 2nd argument is not an integer, then goto the next case.
        If Not StringIsInt($CMDLINE[2]) Then ContinueCase

        ; Create array with filenames starting from index 1.
        Global $aFilenames[$CMDLINE[2] + 1]

        $aFilenames[0] = Int($CMDLINE[2])

        ; Find first filepath that does not exist and set an offset.
        $iOffset = 0

        For $i1 = 1 To 1000
            If Not FileExists(StringReplace($CMDLINE[1], $sTag, $i1, -1)) Then
                $iOffset = $i1 - 1
                ExitLoop
            EndIf
        Next

        ; Assign the array with filenames, replacing tag with an integer.
        For $i1 = 1 To $aFilenames[0]
            $aFilenames[$i1] = StringReplace($CMDLINE[1], $sTag, $i1 + $iOffset, -1)
        Next

    Case Else
        ; Assign default command line array.
        $aFilenames = $CMDLINE
EndSwitch

If $bEnableProgress Then
    ProgressOn(@ScriptName, 'SaveAs')
EndIf

For $i1 = 1 To $aFilenames[0]
    ; Filename to save as.
    $sSaveAsFilename = $aFilenames[$i1]

    ; Print window.
    $hPrint = WinWait("Print")
    ControlClick($hPrint, "", "OK")

    ; Progress window.
    $hProgress = WinWait("Printing")

    ; Save As window.
    $hSaveAs = WinWait("Save Print Output As")

    Do
        Sleep(500)
        ControlSetText($hSaveAs, "", "Edit1", $sSaveAsFilename)
    Until ControlGetText($hSaveAs, "", "Edit1") = $sSaveAsFilename

    Sleep(500)

    If $bEnableProgress Then
        ProgressSet(100 / $aFilenames[0] * $i1, $sSaveAsFilename)
    EndIf

    ControlClick($hSaveAs, "", "Button2")
    AdlibRegister("_ConfirmSaveAs")
    WinWaitClose($hSaveAs)
    AdlibUnRegister("_ConfirmSaveAs")

    ; Wait for the progress window to close.
    WinWaitClose($hProgress)
Next

If $bEnableProgress Then ProgressOff()

Exit

Func _ConfirmSaveAs()
    ; Handle possible msgbox to confirm overwrite.
    If WinExists("Confirm Save As") Then
        ControlClick("Confirm Save As", "", "&Yes")
    EndIf
EndFunc

Func _HelpMsg()
    ; Help message.
    MsgBox(0, @ScriptName, _
     "Automates the standard print dialog from a web browser." & @CRLF & _
     @CRLF & _
     "Syntax:" & @CRLF & _
     "  " & @ScriptName & " filepath [filepath] ..." & @CRLF & _
     "  " & @ScriptName & " filepath integer" & @CRLF & _
     @CRLF & _
     "1st syntax can pass 1 or more filepath arguments." & @CRLF & _
     @CRLF & _
     "2nd syntax replaces the tag " & $sTag & " from right side of the " & _
     "1st argument with an incremented integer (starting from 1). " & _
     "Example: test" & $sTag & ".pdf will start with test1.pdf and end " & _
     "with testN.pdf (which N is the integer set by the 2nd argument)." & @CRLF & _
     @CRLF & _
     "Tested with Firefox 63 on Windows 10.")
EndFunc

文件名|文件路径可以作为参数传递。 如果要另存为 1 个文件名,请使用:

scriptname.exe "C:\SaveFolder\a.pdf"

您还可以在同一次执行中执行多项操作,即:

scriptname.exe "C:\SaveFolder\a.pdf" "C:\SaveFolder\b.pdf" ...

如果你想用一个整数递增文件名,那么即:

scriptname.exe "C:\SaveFolder\a++1.pdf" 3

其中 ++1 将被替换为一个整数,并且将被处理为:

scriptname.exe "C:\SaveFolder\a1.pdf" "C:\SaveFolder\a2.pdf" "C:\SaveFolder\a3.pdf"

第一个参数必须包含字符串 ++1 和 第二个参数必须是要识别的整数 作为要递增的基本文件名。

可以通过使用 /? 作为唯一参数来显示帮助消息框。

同一执行中的许多参数可能不适合控制 在您的 Selenium 脚本中,尽管它是一个选项。

这些 windows 是标准的打印对话框所以不同 Chrome 和 Firefox 之间可能是 none。 "Printing" window 如果你从 即记事本,因此 window 不能被视为标准。

$sSaveAsFilename设置为将要设置的值 进入 "File name:" 的编辑控件,在标题为 window 的 "Save Print Output As".

Opt参数TrayIconDebug会显示当前 当鼠标光标悬停在系统托盘中时 图标。 因此,如果它停滞不前,那么您可以追踪它是否被困住了。 这是一个可选的函数调用。

这是在 Windows 10 虚拟机中测试的 使用 Firefox 63。 windows 显得很慢 在显示上,这就是为什么检查 Edit1 正确的字符串,然后再继续。替代 是使用 Opt() 和参数 WinWaitDelay at 在 window 显示后大约 1000 暂停, 尽管脚本平均需要更长的时间才能完成。

我添加了一个 AdlibRegister 函数用于重复 测试并且可能仍然有用,因为文件名可能 不知不觉中存在,需要处理

"Printing"window等为主, 只是为了阻止脚本在 打印进度完成。 如果不需要,请删除相关代码。

$check = True
$printClicked = False
$saveClicked = False

While($check)
    If WinExists("Print", "") Then
        Send("{ENTER}")
        $printClicked = True
    EndIf

    If WinExists("Save Print Output As", "") Then
        ControlFocus("Save Print Output As", "", "Edit1")
        Sleep(50)
        ControlSetText("Save Print Output As", "", "Edit1", "H282")
        Sleep(50)
        ControlClick("Save Print Output As", "", "Button2")
        $saveClicked = True
    EndIf
    If($printClicked = True And $saveClicked = True) Then
;~      set the $check here
        ExitLoop
    EndIf
WEnd

如果这不起作用,则表示您没有正确获取控件。 发送 Autoit 工具的输出