Finding all <A> elements on a page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BobRoyAce

    Finding all <A> elements on a page

    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 HtmlElementColl ection
    oCollection = WebBrowser1.Doc ument.GetElemen tsByTagName("A" )

    oCollection.Cou nt 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

    #2
    Re: Finding all &lt;A&gt; elements on a page

    On Nov 13, 7:45 am, BobRoyAce <b...@omegasoft wareinc.comwrot e:
    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 HtmlElementColl ection
        oCollection = WebBrowser1.Doc ument.GetElemen tsByTagName("A" )
    >
    oCollection.Cou nt 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.Co unt" returned 29 items.
    However you can try "Webbrowser1.Do cument.Links.Co unt" to get same
    result.

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

    For Each item As HtmlElement In WebBrowser1.Doc ument.Links
    ' Do whatever you want with found links
    item.GetAttribu te("href")
    Next

    Hope this help,

    Onur Güzel

    Comment

    • BobRoyAce

      #3
      Re: Finding all &lt;A&gt; elements on a page

      Using "A" (anchor) as tag to get links should be correct, as i tried
      to navigate "www.google.com ", "oCollection.Co unt" returned 29 items.
      However you can try "Webbrowser1.Do cument.Links.Co unt" 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.Rea dyState <WebBrowserRead yState.Complete
      Application.DoE vents()
      End While
      And if you want to get all the links using
      "WebBrowser1.Do cument.Links", just loop through it using GetAttribute
      function by specifying "href" as follows:
      >
      For Each item As HtmlElement In WebBrowser1.Doc ument.Links
      ' Do whatever you want with found links
      item.GetAttribu te("href")
      Next
      Thanks...

      Comment

      • kimiraikkonen

        #4
        Re: Finding all &lt;A&gt; elements on a page

        On Nov 21, 4:42 pm, BobRoyAce <b...@omegasoft wareinc.comwrot e:
        Using "A" (anchor) as tag to get links should be correct, as i tried
        to navigate "www.google.com ", "oCollection.Co unt" returned 29 items.
        However you can try "Webbrowser1.Do cument.Links.Co unt" 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.Rea dyState <WebBrowserRead yState.Complete
              Application.DoE vents()
            End While
        >
        And if you want to get all the links using
        "WebBrowser1.Do cument.Links", just loop through it using GetAttribute
        function by specifying "href" as follows:
        >
         For Each item As HtmlElement In WebBrowser1.Doc ument.Links
        ' Do whatever you want with found links
         item.GetAttribu te("href")
        Next
        >
        Thanks...
        You can use the code in my previous post in Webbrowser's
        DocumentComplet ed 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

        Comment

        • BobRoyAce

          #5
          Re: Finding all &lt;A&gt; elements on a page

          Unless you know of a better way to do things, I have found that using
          the DocumentComplet ed 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 DocumentComplet ed 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?

          Comment

          • kimiraikkonen

            #6
            Re: Finding all &lt;A&gt; elements on a page

            On Nov 21, 8:10 pm, BobRoyAce <b...@omegasoft wareinc.comwrot e:
            Unless you know of a better way to do things, I have found that using
            the DocumentComplet ed 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 DocumentComplet ed 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.spa ces.live.com/blog/cns!3CA68ED86F5 A5970!376.entry

            Hope this helps,

            Onur Güzel

            Comment

            Working...