如何使用vba excel在网页上单击超链接? [英] How to click on a hyperlink on a web page using vba excel?
问题描述
场景是:
一个特定的网页有一个表,第一列是一个图标,第二列是超链接和几个更多有关超链接描述的列。
现在,我想单击第一行中的超链接(第二列)。
当我们将鼠标悬停在同一行(第一列)中的图标时,将显示相同的超链接。
此网页是动态的,因此表格保持改变。实际上,表格是输入搜索条件的结果。
我从三天起写了很多代码。
这是最新的不起作用:
'获取行的元素ID
使用elementid = ieApp.document.all.Item(abcrow1)
'检查标记名为a的详细信息
设置标记名= ieApp.document.getElementByTagName (a)
对于每个标签在标记名
'获取href
hrefvalue = tag.href.value
的值如果不是IsNull(hrefvalue)则
如果hrefvalue<> JavaScript的:无效(0);然后'这个href用于描述图标
hrefvalue = ieApp.document.href.value
hrefvalue =https://www.secur.com& hrefvalue
ieApp.Navigate hrefvalue
退出
结束如果
结束IF
下一个标签
结束
HTML脚本如下:
; tr id =abcrow1class =e1>
< td class =icon>< / td>
< td>< ul class =xyzid =link>
< li>< a href =javascript:void(0);>< img src =/ nnn.gifborder = 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>
请帮助我。谢谢。
这是否有效?如果没有,hrefvalue_1和hrefvalue_2评估什么?
设置Tagname = ieApp.document.getElementsByTagName(a)
标记名中的每个标签
如果InStr(1,Tag.innertext,DetailOfRow1,vbTextCompare)> 0然后
hrefvalue_1 = Tag'标签可能包含about:,如果是这样删除它
hrefvalue_2 =替换(标签,about:,,1,-1,vbTextCompare)
hrefvalue_3 =https://www.secur.com& hrefvalue_2
ieApp.Navigate hrefvalue
退出
结束如果
下一个标签
如果没有访问url,这是一个很难完成,但这些行应该可以工作...
使用elementid = ieApp.document.all.Item(abcrow1)
'检查标记名为a的详细信息
设置标记名= ieApp.document.getElementByTagName(a)
对于每个标签在标记名
'获取href
的值如果InStr(1,tag.getAttribute(href),javascript:void(0);,vbTextCompare )= 0然后
hrefvalue = tag
hrefvalue =https://www.secur.com& hrefvalue
ieApp.Navigate hrefvalue
退出
结束如果
下一个标签
结束
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.
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
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:
<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.
Does this work? If not, what do hrefvalue_1 and hrefvalue_2 evaluate to?
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...
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
这篇关于如何使用vba excel在网页上单击超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!