2

我已经阅读了很多关于使用 VBA 在 IE 中单击链接的信息,但我无法让它在我的情况下工作。相关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

4

2 回答 2

2

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

CSS 查询

.querySelector方法属于 HTMLDocument。

你可以做:

ieDoc.querySelector("#LinkButton_3 a").Click
于 2018-06-19T19:20:26.607 回答
1
<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
于 2017-01-19T20:12:15.277 回答