【问题标题】:Unable to click at hyperlink on webpage with anchor tag无法单击带有锚标记的网页上的超链接
【发布时间】:2026-01-02 16:15:02
【问题描述】:

在测试了不同的逻辑之后,我终于在Visual Basic for Applications中找到了触发以下属性的正确方法:

我想点击不保持不变的超链接,它会在每次尝试时显示不同的数字和超链接,下面是我的 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 的检查:

【问题讨论】:

    标签: html excel vba internet-explorer href


    【解决方案1】:

    这当然行不通

    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的循环。

    我建议只循环遍历所有链接标签&lt;a&gt;

    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 
    

    【讨论】:

    • 感谢 PEH,但这些代码无法点击链接,甚至也没有指示任何错误:{
    • @samm 请逐步调试您的代码 (F8),看看它是否找到具有特定值的标签以及是否到达步骤 LinkItem.Click。还要确保不要在代码中的任何地方使用On Error Resume Next
    • @PEH 在调试代码时出现运行时错误“70”:权限被拒绝。 :(
    • @samm 好在哪一行?你需要更具描述性我没有水晶球可以看到一切。
    • @PEH,它突出显示以下指令行:“If LinkItem.innerText = Range("K20").Value Then"
    最近更新 更多