无法使用锚标记单击网页上的超链接

时间:2018-04-03 17:13:20

标签: html excel vba internet-explorer href

在测试了不同的逻辑之后,最后我坚持使用 Visual Basic for Applications 来找出触发以下属性的正确方法:

enter image description here 我想点击不相同的超链接,它会在每次下一次尝试时显示带有超链接的不同数字,下面是我的VBA代码:

Dim MyBrowser As InternetExplore
Dim MyHTML_Element As IHTMLElement
Dim myURL As String

Dim htmlInput As HTMLInputElement
Dim htmlColl As IHTMLElementCollection
Dim p As String
Dim link As Object
Dim I As Integer
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument

myURL = "url............."
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate myURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.Document

If htmldoc.all.item(i).innerText = Range("K20").Value Then  ' Range is equal to cell value "4000123486736"
   htmldoc.all.item(i).Click            <------- not woking both lines

请参阅下面的IE检查:

enter image description here

1 个答案:

答案 0 :(得分:0)

当然这不起作用

If htmldoc.all.item(i).innerText = Range("K20").Value Then  ' Range is equalto cell value "4000123486736"
   htmldoc.all.item(i).Click            <------- not woking both lines

因为没有定义i的循环。

我建议仅遍历所有链接标记<a>

Dim LinkItem As Variant
For Each LinkItem In HTMLDoc.getElementsByTagName("a")
    If LinkItem.innerText = Range("K20").Value Then
        LinkItem.Click
        Exit For 'stop looping when link was found
    End If
Next LinkItem