Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I get vb.net to add all links within a particular div from vb browser?

I'm trying to get all links from the div class linksmain.

E.g.

HTML
<div class="linksmain">
<a href="http://www.image1.com">Image</a>
<a href="http://www.image2.com">Image</a>
<a href="http://www.image3.com">Image</a>
</div>



So far I've got this..

Code:

VB
Dim web As New WebBrowser

If web.ReadyState = WebBrowserReadyState.Complete Then
    Dim allelements As HtmlElementCollection = web.Document.All

    For Each webpageelement As HtmlElement In allelements

        ListBox1.Items.Add(webpageelement.GetAttribute("class"))

    Next
End If
Posted

1 solution

That's client side stuff - you would use javascript for that. Here's a javascript function that will return an array containing the IDs of all the elements (that have IDs) in the chosen div. You'll have to enumerate them and decide which children you need to use. This means that all the items you are looking for must have IDs, of course.

JavaScript
function GetAllChildrenIn(parentElement) {
         var children = new Array();
         var numFound = 0;
         try {
             var obj = document.getElementById(parentElement);
             var numChildren = obj.childNodes.length;
         } catch (e) {
             alert('Error setting vars. (' + e.Message + ')'); }

         try {
             for (var count = 0; count < numChildren; count++) {
                 if (obj.childNodes[count].id) {
                     children[numFound] = obj.childNodes[count].id;
                     numFound++;
                 }
             }
         } catch (e) { alert('Error enumerating children. (' + e.Message + ')'); }

         return children;
     }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900