问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/21002756/how-to-click-on-a-hyperlink-on-a-web-page-using-vba-excel