【问题标题】:How to click on a hyperlink on a web page using vba excel?如何使用 vba excel 单击网页上的超链接?
【发布时间】:2014-01-26 23:21:03
【问题描述】:

我是 excel vba 编程的新手。从近 3 天开始,我一直在尝试使用 excel vba 单击网页上的超链接。请帮帮我。因为我是新手,所以我可能犯了很多错误。请原谅我。

场景是: 一个特定的网页有一个表格,第一列是一个图标,第二列是一个超链接,还有几列是关于超链接的一些描述。

现在,我想单击第一行(第二列)中的超链接。 当我们将鼠标悬停在同一行(第一列)中的图标上时,会显示相同的超链接。

此网页是动态的,因此表格会不断变化。实际上该表是输入的搜索条件的结果。

三天以来,我写了很多代码。 这是最新的不工作的:

'获取行的元素id

With elementid = ieApp.document.all.Item("abcrow1")
   'checking for details with tagname as "a"
   Set tagname = ieApp.document.getElementByTagName("a")

   For Each tag In tagname
     'Get value of the href
     hrefvalue = tag.href.value
   If Not IsNull(hrefvalue) Then
     If hrefvalue <> "javascript:void(0);" Then  'this href is usedto describe the icon
        hrefvalue = ieApp.document.href.value

        hrefvalue = "https://www.secur.com" & hrefvalue
        ieApp.Navigate hrefvalue
        Exit For
     End If
   End IF
Next tag
End With

HTML脚本如下:

 <tr id = "abcrow1" class="e1">
   <td class = "icon"></td>
   <td><ul class="xyz" id="link">
       <li><a href = "javascript:void(0);"><img src="/nnn.gif" border = 0 alt = "info"         </a>
       <ul>
       <li><a onclick ="return cursorWait(this);" href = "/xyz/lmo">DetailOfRow1</a></li></ul></td>
   <td style = "text-align:left"><aonclick="return cursorWait(this);" href = "/xyz/lmo">DetailOfRow1</a></td></tr>

请帮帮我。谢谢。

【问题讨论】:

    标签: excel hyperlink webpage vba


    【解决方案1】:

    这行得通吗?如果不是,hrefvalue_1 和 hrefvalue_2 的评估结果是什么?

    Set Tagname = ieApp.document.getElementsByTagName("a")
    For Each Tag In Tagname
        If InStr(1, Tag.innertext, "DetailOfRow1", vbTextCompare) > 0 Then
            hrefvalue_1 = Tag     ' tag may contain "about:", if so remove it
            hrefvalue_2 = Replace(Tag, "about:", "", 1, -1, vbTextCompare)
            hrefvalue_3 = "https://www.secur.com" & hrefvalue_2
            ieApp.Navigate hrefvalue
            Exit For
        End If
    Next Tag
    

    如果没有访问 url,就有点难以完成,但是这些方面的东西应该可以工作......

    With elementid = ieApp.document.all.Item("abcrow1")
     'checking for details with tagname as "a"
     Set tagname = ieApp.document.getElementByTagName("a")
    
     For Each tag In tagname
       'Get value of the href
      If InStr(1, tag.getAttribute("href"), "javascript:void(0);", vbTextCompare) = 0 Then       
         hrefvalue = tag
         hrefvalue = "https://www.secur.com" & hrefvalue
         ieApp.Navigate hrefvalue
         Exit For
      End If
     Next tag
    End With
    

    【讨论】:

    • 谢谢。采纳您的建议后,我的很多问题都得到了解决。但我仍然无法导航到 html 代码最后一行中提到的链接。 `
    • 如何获取属性样式?这是对的吗,因为它似乎不起作用:If Instr(1,tag.getAttribute("Style"),"tex-align:left",vbTextCompare)&lt;&gt;0 Then 导航到 href 中的链接。
    猜你喜欢
    • 2014-08-29
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多