Finding all <A> elements on a page

B

BobRoyAce

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

kimiraikkonen

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
 
B

BobRoyAce

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

kimiraikkonen

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



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
 
B

BobRoyAce

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

kimiraikkonen

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
 
B

BobRoyAce

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

After checking the blog entry, I'm still not sure why my simple loop
wouldn't work as I'm checking for WebBrowser1.ReadyState <>
WebBrowserReadyState.Complete over and over again until it does equal
Complete. From what I've read, frames wouldn't be the cause of my
problem because ReadyState won't equal Complete until all frames have
been loaded right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top