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

Viewed 290

Using AutoIt I want to get PDF files from a website, which requires inputting a date value and subsequently, clicking of a Submit button. _IEFormElementSetValue() can change the date value but the site doesn't register this change. Even after clicking the Submit button using $oTag.click() it won't load any PDF files.

The site does register the date value change if I give it focus first using .focus(). This works until I minimize the window (my script works only if target window is active and in focus, like WinActivate() does). But I can't use my system this way; I made it open to enter dates to then get minimized again, but that is annoying me too.

How to make it work while target window is minimized? Here is my code (downloading PDF files I already worked out):

#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

Ignore the include files. Try without .focus() first and you can't click the Submit button (site doesn't register changes), so no PDF files appear. Click each input box (after program sets the value without focus) and then click the Submit button; this works.

2 Answers

If you select inspect element on a date picker field you will see it has 3 event triggers.

Firefox Inspect Element

Trigger those events like this:

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

Please use AutoIt's help file. Below is your code cleaned up :

#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

You can use $oTag.value= instead of _IEFormElementSetValue. This works perfectly without focus:

$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'  
Related