浏览器窗口保持最小化时自动化网页(需要主动聚焦)

时间:2017-10-04 10:42:15

标签: internet-explorer autoit

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

如果我首先使用.focus()给予焦点,网站会注册日期值更改。这有效,直到我最小化窗口(我的脚本仅在目标窗口处于活动状态且处于焦点时起作用,如WinActivate()那样)。但我不能这样使用我的系统;我打开它来输入日期然后再次最小化,但这也让我感到烦恼。

如何在目标窗口最小化的情况下使其工作?这是我的代码(下载我已经编写过的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文件。单击每个输入框(在程序设置无焦点的值后),然后单击“提交”按钮;这很有效。

1 个答案:

答案 0 :(得分:2)

如果在日期选择器字段中选择了inspect元素,您将看到它有3个事件触发器。

Firefox Inspect Element

触发这样的事件:

$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