使用VBA进行Internet Explorer自动化输入事件

6

我正在尝试使用Microsoft Access 2003中的自动化来控制Internet Explorer 9,使用数据库数据填写表单。

输入在浏览器中触发一个事件,该事件会验证数据并将保存按钮显示出来。如果我使用sendkeys,则会触发事件,但是我发现sendkeys非常不可靠。如果我更改元素的值,然后使用.fireevent("onchange"),则什么也不会发生,甚至没有错误。

我的问题是,我该如何触发事件。或者,我怎样才能找到正在运行的JavaScript代码。是否有IE的调试插件可以告诉我哪个事件被触发了?如果有,我能否自己运行脚本?

以下是我的代码:

    Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.Navigate "https://extranet.website.com/Planning/Edition/Periodic?language=en"

Do While IE.ReadyState <> 4 Or IE.Busy = True
    DoEvents
Loop


'log in
If IE.Document.Title = "website- access" Then
    IE.Document.getElementById("login_uid").Value = "username"
    IE.Document.getElementById("login_pwd").Value = "password"
    IE.Document.all("ButSubmit").Click
    Do While IE.ReadyState <> 4 Or IE.Busy = True
        DoEvents
    Loop
End If

Do While Not RstAvailability.EOF
    StartDate = RstAvailability!AvailDate
    IE.Document.getElementById("periodStart").Value = Format(StartDate, "dd mmm yy")
    IE.Document.getElementById("periodEnd").Value = Format(StartDate, "dd mmm yy")
    Set LinkCollection = IE.Document.GetElementsByTagName("A")
    For Each link In LinkCollection
        If link.innertext = "Add" Then
            link.Click
            Exit For
        End If
    Next
    Do While IE.ReadyState <> 4 Or IE.Busy = True
        DoEvents
    Loop


    Set objRows = IE.Document.GetElementsByTagName("tr")
    If RstAvailability!RoomType = "DTW" Then
        n = 0
        While n < objRows.Length
            If Trim(objRows(n).Cells(0).innertext) = "Single Room" Then
                For i = 1 To 7
                    'objRows(n).FireEvent ("onchange")
                    'objRows(n).Cells(i).GetElementsByTagName("input")(0).Focus
                    'SendKeys Format(RstAvailability!roomcount - RstAvailability!RoomsSold, "0") & "{TAB}"
                    objRows(n).Cells(i).GetElementsByTagName("input")(0).Value = Format(RstAvailability!roomcount - RstAvailability!RoomsSold, "0")
                    objRows(n).Cells(i).GetElementsByTagName("input")(0).fireevent ("onchange")
                    Do While IE.ReadyState <> 4 Or IE.Busy = True
                        DoEvents
                    Loop
                 Next i
            End If
            n = n + 1
        Wend
    End If

    Set objButtons = IE.Document.getelementsbyname("savePlanning")
    objButtons(0).Click

    Do While IE.ReadyState <> 4 Or IE.Busy = True
        DoEvents
    Loop

    newtime = Now + TimeValue("0:00:10")
    Do While True
        If Now > newtime Then Exit Do
    Loop

    RstAvailability.MoveNext
Loop

输入字段的HTML代码如下:
<tr class="first" roomId="30494" articleId="0" type="Availability" readonly="False">

<div>

  <span class="roomName">

    Single Room

  </span>



</div>

<span class="data">

<input id="Availabilities" name="Availabilities" type="text" value="" />

</span>

<span class="data">

<input id="Availabilities" name="Availabilities" type="text" value="" />

</span>

Thanks!

2个回答

14

在为此苦苦思索了几天之后,答案实际上非常简单,但几乎不可能在 MSDN 或其他任何网络文档中找到。

在更改输入字段的值之前,您需要将焦点设置在该字段上。 在更改值之后,您需要将焦点设置为另一个字段。 显然,事件会在失去焦点时触发。 因此,代码应该像这样:

        n = 0
    While n < objRows.Length
        If Trim(objRows(n).Cells(0).innertext) = "Family Room" Then
            For i = 1 To 7
                objRows(n).Cells(i).GetElementsByTagName("input")(0).Focus
                objRows(n).Cells(i).GetElementsByTagName("input")(0).Value = Format(RstAvailability!roomcount - RstAvailability!RoomsSold, "0")
            Next i
            objRows(n).Cells(1).GetElementsByTagName("input")(0).Focus

        End If
        n = n + 1
    Wend

我发现这个方法是通过查看IE9的无障碍性MSDN文档获得的建议,它建议为残障用户设置焦点。我想试一试,结果起作用了。希望这能帮助其他人。

Dave


谢谢,这对我很有帮助。 - BlueTrin

1
如果你想在"Next i"这一行之前(在IE9中)插入以下行,即使你不失去焦点,它也应该起作用吗?

objRows(n).Cells(i).GetElementsByTagName("input")(0).Click


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接