2017-01-19 802 views
0

我已经阅读了很多关于使用VBA在IE中单击链接的信息,但是我无法在我的情况下使用它。 相关的HTML代码如下:使用VBA点击IE网页中的链接/按钮

<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

+0

在试#1,'button.Children(0).Click'工作? –

+0

马特,你完全真棒!这确实奏效。帮助我了解button.Click和button.Children(0).Click之间的区别。再次感谢。 – TMc

+0

你真正想要点击的是div内的锚点(a)标签。因此,你想要那个div的第一个孩子。这有帮助吗? –

回答

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

要点击什么是锚(一)标签,而不是股利。所以,你可以找到与它的ID的div,然后点击其唯一的孩子与

button.Children(0).Click