如何使用vba excel点击网页上的超链接?

时间:2014-01-08 17:41:40

标签: excel excel-vba hyperlink webpage vba

我是excel vba编程的新手。我一直试图使用excel vba点击网页上的超链接,差不多3天了。请帮帮我。由于我是新手,我可能犯了很多错误。请原谅我。

情景是: 特定网页有一个表格,第一列作为图标,第二列是超链接,还有一些列,其中包含有关超链接的一些描述。

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

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

我从三天开始写了很多代码。 这是最新的不起作用:

'获取行的元素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>

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:0)

这有用吗?如果没有,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

如果没有访问网址,最终确定有点困难,但这些内容应该有效......

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