VBA:输入单击下一个类似id的getElementbyId

时间:2013-02-26 09:03:48

标签: vba automation getelementbyid

我正面临有关我的VBA脚本的问题。 我的问题是,如何点击具有与前一个相同ID的输入按钮的按钮。唯一不同的是onclick值...... 我的脚本适用于第一个ADD按钮,但是当页面加载时我不知道如何使我的代码单击新的ADD按钮(其ID与前一个相同)。看下面的代码:

首次加载HTML页面:

<input id="btnEdit" class="button" onclick="openExpenseForm(1,'ExpenseCard.aspx?ExpenseFormID=c78f4ac6-63fc-4a47-a06a-a4eafe3ded08&ExpenseCardID=&Row=1&Action=New&Enabled=True');"
                        type="button" value="ADD" />

第二次加载HTML页面:

<input id="btnEdit" class="button" onclick="openExpenseForm(1,'ExpenseCard.aspx?ExpenseFormID=c78f4ac6-63fc-4a47-a06a-a4eafe3ded08&ExpenseCardID=&Row=1&Action=New&Enabled=True');"
                        type="button" value="EDIT" />
<input id="btnEdit" class="button" onclick="openExpenseForm(2,'ExpenseCard.aspx?ExpenseFormID=c78f4ac6-63fc-4a47-a06a-a4eafe3ded08&ExpenseCardID=&Row=1&Action=New&Enabled=True');"
                        type="button" value="ADD" />

VBA:

    Dim oIE As Object
Set oIE = CreateObject("InternetExplorer.Application")
    oIE.Visible = True
    URL = "http://netsteps/expense/ExpenseForm.aspx?Action=New&EmployeeID=" & ActiveSheet.Cells(1, 3)
    oIE.Navigate (URL)

AppActivate oIE

Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
Dim htmlDoc As MSHTML.HTMLDocument ' html object lib
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Set objIE = New SHDocVw.InternetExplorer

With objIE
With ActiveSheet

    Do While oIE.Busy: DoEvents: Loop
    Do While oIE.readyState <> 4: DoEvents: Loop


    oIE.document.getElementById("ctl00_Main_txtExpenseBusinessPurpose").Value = CStr(.Cells(2, 3))

    Do While (Not IsEmpty(.Cells(lin, 1)))

    Set htmlDoc = oIE.document
    Set htmlColl = htmlDoc.getElementsByTagName("input")

        For Each htmlInput In htmlColl
            If (htmlInput.Value = Trim("ADD") And Trim(htmlInput.Type) = "button") Then
                htmlInput.Click
            End If
        Next htmlInput

        Do While oIE.Busy: DoEvents: Loop
        Do While oIE.readyState <> 4: DoEvents: Loop
        ' Process 
        'End of all loops...            

我有点卡住,任何帮助都会感激不尽。 祝你有美好的一天, 感谢

1 个答案:

答案 0 :(得分:0)

html文档中的

id属性应该是唯一的,但您在两个元素中分配相同的值btnEdit。消除它们,例如。通过附加按钮的值属性(btnEdit_ADDbtnEdit_EDIT)的内容。如果您需要表示按钮是独一无二的,请使用data- - 属性(有关详细信息,请参阅here)。希望有所帮助,问候