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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21002756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:31:48  来源:igfitidea点击:

How to click on a hyperlink on a web page using vba excel?

excelexcel-vbahyperlinkwebpagevba

提问by user3174245

I am new to excel vba programing. I have been trying to click on a hyperlink on a web page using excel vba from almost 3 days now. Please help me out. Since I am new to this I might have made a lot of blunders. Pardon me for that.

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

The scenario is : A particular web page has a table with first column as an icon, second column is a hyperlink and a few more columns with some description about the hyperlink.

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

Now, I want to click on the hyperlink in the first row (second column). When we hover over the icon present in the same row(in first column) the same hyperlink is displayed.

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

This webpage is dynamic, hence the table keeps changing. Actually the table is a result of a search criteria entered.

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

I have written a lot of codes from three days. This is the latest one thats not working:

我从三天时间写了很多代码。这是最新的一个不起作用:

'Get element id for the row

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

The HTML script is as follows:

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>

Please help me out. Thank you.

请帮帮我。谢谢你。

采纳答案by ron

Does this work? If not, what do hrefvalue_1 and hrefvalue_2 evaluate to?

这行得通吗?如果不是,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

It's a little hard to finalize without acces to the url, but something along these lines should work...

在不访问 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