Connecting Tech Pros Worldwide Help | Site Map

Finding all <A> elements on a page

BobRoyAce
Guest
 
Posts: n/a
#1: Nov 13 '08
There is a web page that has a bunch of links on it (i.e. "<A
HREF=..."). I am trying to automate clicking on one in a WebBrowser
control. However when I execute the following code:

Dim oCollection As HtmlElementCollection
oCollection = WebBrowser1.Document.GetElementsByTagName("A")

oCollection.Count is ZERO. I am assuming that I'm not using the
correct Tag Name. I also tried "a" and "link" but they also return no
elements.

What is the correct Tag Name to use?

Is there a list of the possible tag names somewhere?
kimiraikkonen
Guest
 
Posts: n/a
#2: Nov 13 '08

re: Finding all <A> elements on a page


On Nov 13, 7:45*am, BobRoyAce <b...@omegasoftwareinc.comwrote:
Quote:
There is a web page that has a bunch of links on it (i.e. "<A
HREF=..."). I am trying to automate clicking on one in a WebBrowser
control. However when I execute the following code:
>
* * Dim oCollection As HtmlElementCollection
* * oCollection = WebBrowser1.Document.GetElementsByTagName("A")
>
oCollection.Count is ZERO. I am assuming that I'm not using the
correct Tag Name. I also tried "a" and "link" but they also return no
elements.
>
What is the correct Tag Name to use?
>
Is there a list of the possible tag names somewhere?
Hi Bob,
Using "A" (anchor) as tag to get links should be correct, as i tried
to navigate "www.google.com", "oCollection.Count" returned 29 items.
However you can try "Webbrowser1.Document.Links.Count" to get same
result.

And if you want to get all the links using
"WebBrowser1.Document.Links", just loop through it using GetAttribute
function by specifying "href" as follows:

For Each item As HtmlElement In WebBrowser1.Document.Links
' Do whatever you want with found links
item.GetAttribute("href")
Next

Hope this help,

Onur Güzel
BobRoyAce
Guest
 
Posts: n/a
#3: Nov 21 '08

re: Finding all <A> elements on a page


Using "A" (anchor) as tag to get links should be correct, as i tried
Quote:
to navigate "www.google.com", "oCollection.Count" returned 29 items.
However you can try "Webbrowser1.Document.Links.Count" to get same
result.
>
Yea, thought "A" for tag name should be fine. I think it's an issue
with the browser not finished downloading the document.
I am using the following code, running it before I check for the "A"
tags. Should it work?

While WebBrowser1.ReadyState <WebBrowserReadyState.Complete
Application.DoEvents()
End While
Quote:
And if you want to get all the links using
"WebBrowser1.Document.Links", just loop through it using GetAttribute
function by specifying "href" as follows:
>
For Each item As HtmlElement In WebBrowser1.Document.Links
' Do whatever you want with found links
item.GetAttribute("href")
Next
Thanks...
kimiraikkonen
Guest
 
Posts: n/a
#4: Nov 21 '08

re: Finding all <A> elements on a page


On Nov 21, 4:42*pm, BobRoyAce <b...@omegasoftwareinc.comwrote:
Quote:
Quote:
Using "A" (anchor) as tag to get links should be correct, as i tried
to navigate "www.google.com", "oCollection.Count" returned 29 items.
However you can try "Webbrowser1.Document.Links.Count" to get same
result.
>
Yea, thought "A" for tag name should be fine. I think it's an issue
with the browser not finished downloading the document.
I am using the following code, running it before I check for the "A"
tags. Should it work?
>
* * While WebBrowser1.ReadyState <WebBrowserReadyState.Complete
* * * Application.DoEvents()
* * End While
>
Quote:
And if you want to get all the links using
"WebBrowser1.Document.Links", just loop through it using GetAttribute
function by specifying "href" as follows:
>
Quote:
*For Each item As HtmlElement In WebBrowser1.Document.Links
' Do whatever you want with found links
*item.GetAttribute("href")
Next
>
Thanks...
You can use the code in my previous post in Webbrowser's
DocumentCompleted event handler if your wish is to get all the links
when webbrowser finishes loading. When loading finishes, now it should
count all the elements that refer to links.

Thanks,

Onur Güzel
BobRoyAce
Guest
 
Posts: n/a
#5: Nov 21 '08

re: Finding all <A> elements on a page


Unless you know of a better way to do things, I have found that using
the DocumentCompleted method is a pain in the neck because there are a
bunch of different documents/pages that ultimately get loaded and, so,
the application needs to keep track of which one was just
loaded...this because the single DocumentCompleted event is shared by
all documents/pages. Why is it that the code that posted, waiting for
ReadyState to equal Complete, doesn't work? Perhaps the ReadyState is
set before the document/page has been downloaded?
kimiraikkonen
Guest
 
Posts: n/a
#6: Nov 21 '08

re: Finding all <A> elements on a page


On Nov 21, 8:10*pm, BobRoyAce <b...@omegasoftwareinc.comwrote:
Quote:
Unless you know of a better way to do things, I have found that using
the DocumentCompleted method is a pain in the neck because there are a
bunch of different documents/pages that ultimately get loaded and, so,
the application needs to keep track of which one was just
loaded...this because the single DocumentCompleted event is shared by
all documents/pages. Why is it that the code that posted, waiting for
ReadyState to equal Complete, doesn't work? Perhaps the ReadyState is
set before the document/page has been downloaded?
Well, then you'd better check out this blog entry, it demonstrates how
to add an event that is raised when ReadyState indicates the browser
control is completed loading.
It's in C#, but you can convert to VB.NET of course using with an
online converter or by yourself.

http://afjohansson.spaces.live.com/blog/cns!3CA68ED86F5A5970!376.entry

Hope this helps,

Onur Güzel
Closed Thread