【发布时间】:2017-01-19 16:30:55
【问题描述】:
我已经阅读了很多关于使用 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
【问题讨论】:
-
在尝试 #1 中,
button.Children(0).Click有效吗? -
马特,你太棒了!!那确实奏效了。帮助我了解 button.Click 和 button.Children(0).Click 之间的区别。再次感谢。
-
你真正想要点击的是div里面的东西,锚(a)标签。因此,您想要该 div 的第一个孩子。这有帮助吗?
-
如果你不介意,我想我会把解决方案及其解释作为答案。
-
我当然不介意。
标签: vba excel web-scraping ie-automation