Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to click an anchor with visual basic

Status
Not open for further replies.

ChrisNome

IS-IT--Management
Mar 22, 2011
47
0
0
US
the HTML DOM says that there are two ways of accessing anchor elements in an HTML document. 1) .getElementsByID and 2) cycling through the HTMLDoc.anchors iHTML collection.

Using VBA, I'm trying to write code that will click an anchor (a "a" element right?) given certain conditions and I can't seem to get 2 to work. I can get 1) to work:

ex.

set elmLink = htmldoc.getelementbyID("-----")
elmLink.click

But this is very case specific and the link I want to click doesn't always have an ID property listed in the source code. Most often it is an "a" tag, and only has a class and href listed from which to work with.

This is fine, the shown text almost always contains a phrase. But I can seem to cycle through the anchors in any way.

I've tried:

for each elmLink in htmldoc.getElementsByTagName("a")
if instr(elmLink.innerText, "[phrase]") then
elmLink.click
exit for
end if
next

and i've tried

for each elmLink in htmldoc.anchors
if instr(elmLink.innerText, "[phrase]") then
elmLink.click
exit for
end if
next

and i've tried

for i = 0 to UBound(htmldoc.anchors, 2)
set elmLink = htmldoc.anchors(, i)
if instr(elmLink.innerText, "[phrase]") then
elmLink.click
exit for
end if
next

..and none of these 3 do anything, the last one doesn't even run without errors. I guess .anchors([name], [index]) is too ambiguous for me. Any thoughts? thx

CN

 
Perhaps posting in forum329 would be a better choice.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Or maybe forum707

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
It sounds like you don't have control over the html document content. Yes, the DOM allows you to extract HTML elements IF the elements are within the same document as the vbs code that uses the DOM. If the elements are external to the vbs, I'm not so certain that you'll be able to use DOM methods to get them.

-Geates

In the voice of the Dos Equis Most Interesting Man in the World: "I don't always test my code. But when I do, I do it in production
 
i solved it for anyone that has a similar problem..

the desired link I wanted to click with the "a" tag was in the links collection, not the anchors, contrary to the information on w3schools.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top