【问题标题】:Automate webpage (requiring active focus) while browser window remains minimized在浏览器窗口保持最小化的情况下自动化网页(需要活动焦点)
【发布时间】:2018-03-15 17:30:19
【问题描述】:

使用 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 文件。单击每个输入框(程序设置值后没有焦点),然后单击提交按钮;这行得通。

【问题讨论】:

    标签: internet-explorer autoit


    【解决方案1】:

    如果您在日期选择器字段中选择检查元素,您将看到它有 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
    

    【讨论】:

      【解决方案2】:

      您可以使用$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'  
      

      【讨论】:

      • 它不像你说的那样工作,伙计..即使最大化也不工作..它只有在首先有焦点时才工作......你或我的编码都没有问题..这是网站特有的问题,所以仍在等待正确答案..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 2012-06-18
      • 2017-08-09
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多