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
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