Home / Software / Computer programming / how to click an anchor with visual basic
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: how to click an anchor with visual basic  (Read 382 times)
piyush123
Topic Starter
Starter



Posts: 3

Experience: Beginner
OS: Unknown

« on: April 15, 2011, 10:49:53 AM »

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


________________________


No spam, thanks.
    -- kpac

« Last Edit: April 15, 2011, 10:58:50 AM by kpac » IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,874

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #1 on: April 15, 2011, 11:52:52 AM »

for each is working fine for me in VB6. chances are it's the Instr causing the most problems (aside from the last example, where you try to use UBound on a collection. That will never work.

for Instr you might need to make the comparison case insensitive.

Code: [Select]
If InStr(1,elmLink.innerText, "[phrase]",vbCompareText)>0 Then
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Pages: [1] - (Top) Print 
Home / Software / Computer programming / how to click an anchor with visual basic « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.096 seconds with 21 queries.