如何将Click事件添加到Excel VBA上的子菜单项?

时间:2016-08-14 12:16:42

标签: vba

我有一个网页,在该页面上有一个菜单项。我想点击子菜单项。 HTML是:

<a href="/apex/COSCaseCreatePage?oMenu=Cases_Submit Case&pagename=COSCaseCreatePage">Submit Case</a>

元素的xpath是:.//*[@id='liSubmitCase']/a

我希望能够点击此内容。

1 个答案:

答案 0 :(得分:1)

如果没有其他细节,很难推荐更好的方法。但是,下面写的内容应该有效。

Sub TestFind()
    Dim Elements As Object
    Dim Element  As Object

    'I'm assuming you already have a pointer
    'to Internet Explorer

    Set Elements = IE.Document.getElementsByTagName("a") ' get all 'a' tags

    For Each Element In Elements
        On Error Resume Next ' you may get an error here
                             ' the object may not have an innertext property
        If Element.InnerText = "Submit Case" Then ' The text you are looking for
            Element.Click ' Click the element
            Exit For ' Exit for loop
        End If
    Next Element

End Sub