Internet Explorer的自动化与VBA输入事件 [英] Internet Explorer Automation with VBA input events

查看:1987
本文介绍了Internet Explorer的自动化与VBA输入事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用自动化从Microsoft Access 2003控制Internet Explorer 9通过数据库中的数据来完成的一种形式。

am trying to use automation in from Microsoft Access 2003 to control Internet Explorer 9 to complete a form using database data.

输入在验证数据,并保存按钮可见浏览器触发一个事件。如果我使用的SendKeys触发事件;然而,我发现的SendKeys是非常不可靠的。如果我改变元素的值,然后使用.fireevent(的onchange),没有任何反应 - 即使不是一个错误。

The input fires an event in the browser which validates the data and makes the save button visible. If I use sendkeys the event is triggered; however, I have found sendkeys to be very unreliable. If I change the value of the element and then use .fireevent ("onchange"), nothing happens – not even an error.

我的问题是,我怎么触发事件。或者说,我怎么能找出什么的JavaScript运行。是否有插件的IE浏览器调试类型它会告诉我什么事件被触发?如果是这样,我能运行该脚本自己?

My question is, how do I fire the event. Or, how can I find out what javascript is running. Is there a debug type of addin for IE which will tell me what event is fired? If so, can I just run the script myself?

我的code如下。

    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是:

The html of the input fields are:

<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>

谢谢!

推荐答案

出汗多这几天后,答案其实很简单,但几乎不可能在MSDN上的文档中找到或其他地方在网络上。

After sweating over this for a few days, the answer was actually very simple but nearly impossible to find in any documentation on MSDN or anywhere else on the web.

在您更改输入字段的值,你的净设置重点放在现场。您更改值后,您需要将焦点设置到另一个字段。显然,事件触发焦点的损失。因此,code应该是这样的:

Before you change the value of the input field, you net to set the focus on that field. After you change the value, you need to set the focus to another field. Apparently, the events fire on the loss of focus. Therefore, the code should look like this:

        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文档。有人建议在设置残障人士关注的焦点。我只是想我会给这是一个尝试,它的工作。我希望这可以帮助别人。

The way I found this was by looking at some MSDN documentation about accessibility for IE9. It was advising on setting the focus for disabled users. I just thought I would give this a try and it worked. I hope this helps someone else.

戴夫

这篇关于Internet Explorer的自动化与VBA输入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆