使用VBA点击IE网页中的link/button

Using VBA to click on a link/button in an IE web page

我已经阅读了很多有关使用 VBA 在 IE 中单击 link 的信息,但我无法在我的情况下使用它。 相关HTML代码如下:

<div id="LinkButton_3" widgetId="LinkButton_3">
    <a class="Row_Icon" data-dojo-attach-point="linkNode" data-dojo-attach-event="onClick:_onLinkButtonClicked">Company XYZ. - #12345</a>
</div>

我尝试过的 VBA 代码,记录了 3 次不同的尝试,如下所示:

Dim ieApp           As SHDocVw.InternetExplorer
Dim ieDoc           As MSHTML.HTMLDocument
Dim button          As HTMLInputButtonElement
Dim div             As HTMLDivElement

' Create a new instance of IE
    Set ieApp = New InternetExplorer

' Uncomment this line for debugging purposes.
    ieApp.Visible = True
' Go to the page we're interested in.
    ieApp.Navigate "MyURL.com"


    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

    Set ieDoc = ieApp.Document
' Try #1.
' Nothing happened on the web page after these 3 lines were executed.
        Set button = ieDoc.getElementById("LinkButton_3")
        button.Focus
        button.Click
' Try #2
' Nothing happens - button is not clicked.
        Set div = ieDoc.getElementById("LinkButton_3")
        div.FireEvent "onClick"
' Try #3
' Nothing happens - button is not clicked.
        div.Click

' Close the instance of IE.
        ieApp.Quit
' Clean up.
        Set ieApp = Nothing
        Set ieDoc = Nothing

任何关于我可能做错了什么的想法或其他建议将不胜感激。

TMc

<div id="LinkButton_3" widgetId="LinkButton_3">
    <a class="Row_Icon" data-dojo-attach-point="linkNode" data-dojo-attach-event="onClick:_onLinkButtonClicked">Company XYZ. - #12345</a>
</div>

您要点击的是锚 (a) 标签,而不是 div。因此,您可以找到带有 ID 的 div,然后单击其唯一的带有

的子节点
button.Children(0).Click

您还可以使用 #LinkButton_3 a 的 CSS querySelector。这是带有 id LinkButton_3 的元素中的 a 标记。 “#”表示 id。 “a”表示 a 内的标签。

.querySelector 方法属于 HTMLDocument。

你可以这样做:

ieDoc.querySelector("#LinkButton_3 a").Click