在浏览器 window 保持最小化的情况下自动化网页(需要活动焦点)

Automate webpage (requiring active focus) while browser window remains minimized

使用 AutoIt 我想从网站获取 PDF 文件,这需要输入日期值,然后单击提交按钮。 _IEFormElementSetValue() 可以更改日期值,但网站不会记录此更改。即使在使用 $oTag.click() 单击提交按钮后,它也不会加载任何 PDF 文件。

如果我首先使用 .focus() 给它焦点,该网站确实会记录日期值更改。这一直有效,直到我最小化 window(我的脚本仅在目标 window 处于活动状态且处于焦点状态时才有效,就像 WinActivate() 一样)。但是我不能这样使用我的系统;我打开它可以输入日期,然后再次最小化,但这也让我很烦。

如何在目标 window 最小化时让它工作?这是我的代码(下载我已经计算出的 PDF 文件):

#include <String.au3>
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Array.au3>
#include <Date.au3>
#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <InetConstants.au3>

$oIE      = _IECreateEmbedded()
$hGUI     = GUICreate('Embedded', @DesktopWidth - 50, @DesktopHeight - 100, 10, 10)
$btSearch = GUICtrlCreateButton('GO', 500, 10, 60, 25)
$oIEobj   = GUICtrlCreateObj($oIE, 10, 150, @DesktopWidth / 2, @DesktopHeight - 250)
GUISetState()
Global $URL = 'https://sanduskyoh.glyphreports.com'

While 1
    Local $msg = GUIGetMsg()

    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $btSearch
            Select
                Case StringInStr($URL, 'glyphreport')
                    While 1
                        glyph()
                    WEnd
            EndSelect
    EndSwitch

    Sleep(500)
WEnd

Func glyph()
    _IENavigate($oIE, $URL)
    Sleep(100)
    Local $oTags = _IETagNameGetCollection($oIE, 'input')

    For $oTag In $oTags
        If $oTag.GetAttribute('id') = "startdatepicker" Then
            Sleep(100)
            $oTag.focus
            _IEFormElementSetValue($oTag, '10/01/2017')
        EndIf
        If $oTag.GetAttribute('id') = "enddatepicker" Then
            Sleep(100)
            $oTag.focus
            _IEFormElementSetValue($oTag, '10/04/2017')
        EndIf
    Next

    Sleep(100)
    Local $oTags = _IETagNameGetCollection($oIE, 'span')

    For $oTag In $oTags
        If $oTag.innerText = 'Upload Date' Then
            $oTag.click()
        EndIf
    Next

    Sleep(100)
    Local $oTags = _IETagNameGetCollection($oIE, 'button')

    For $oTag In $oTags
        If $oTag.GetAttribute('id') = 'reportDateTypeSearchButton' Then
            $oTag.click()
        EndIf
    Next

    Sleep(100000)
EndFunc

忽略包含文件。首先尝试不使用 .focus(),您无法单击提交按钮(站点不注册更改),因此不会出现 PDF 文件。单击每个输入框(程序设置值后无焦点),然后单击提交按钮;这有效。

您可以使用 $oTag.value= 而不是 _IEFormElementSetValue。这在没有焦点的情况下工作得很好:

$oTag = _IEGetObjById($oIE,"startdatepicker")
if not @error then $oTag.value = '10/01/2017'

$oTag = _IEGetObjById($oIE,"enddatepicker")
if not @error then $oTag.value = '10/04/2017'  

如果您 select 检查日期选择器字段上的元素,您将看到它有 3 个事件触发器。

像这样触发那些事件:

$oStartDatePicker.fireEvent("onblur")
$oStartDatePicker.fireEvent("onfocus")
$oStartDatePicker.fireEvent("onkeydown")

请使用AutoIt's help file。以下是您清理后的代码:

#include <String.au3>
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Array.au3>
#include <Date.au3>
#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <InetConstants.au3>

$oIE      = _IECreateEmbedded()
$hGUI     = GUICreate('Embedded', @DesktopWidth - 50, @DesktopHeight - 100, 10, 10)
$btSearch = GUICtrlCreateButton('GO', 500, 10, 60, 25)
$oIEobj   = GUICtrlCreateObj($oIE, 10, 150, @DesktopWidth / 2, @DesktopHeight - 250)
GUISetState()

While True

    Local $msg = GUIGetMsg()

    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btSearch
            glyph()
    EndSwitch

WEnd

Func glyph()

    _IENavigate($oIE, 'https://sanduskyoh.glyphreports.com')

    $oStartDatePicker = _IEGetObjById($oIE, "startdatepicker")
    $oStartDatePicker.value = '10/01/2017'
    $oStartDatePicker.fireEvent("onfocus")
    $oStartDatePicker.fireEvent("onkeydown")
    $oStartDatePicker.fireEvent("onblur")

    $oStopDatePicker = _IEGetObjById($oIE, "enddatepicker")
    $oStopDatePicker.value = '10/04/2017'
    $oStopDatePicker.fireEvent("onfocus")
    $oStopDatePicker.fireEvent("onkeydown")
    $oStopDatePicker.fireEvent("onblur")

    Sleep(100)

    Local $oTags = _IETagNameGetCollection($oIE, 'span')
    For $oTag In $oTags
        If $oTag.innerText == 'Upload Date' Then
            $oTag.click
            ExitLoop
        EndIf
    Next

    Sleep(100)

    $oBtn = _IEGetObjById($oIE, "reportDateTypeSearchButton")
    $oBtn.click

EndFunc   ;==>glyph