views:

912

answers:

1

I want to find a div with the class name XYZ then in it i want to loop through a bunch of elements named ABC. Then grab the links (a href) inside and possibly other information. How do i find the div with XYZ from webBrowser1.Document.Links and any subitems i want?

+3  A: 

First you said you want to find a div with the class name XYZ, so why are you looking in webBrowser1.Documnet.Links? Find the Div first, then get to the links within it.

HtmlDocument doc = webBrowser.Document;
            HtmlElementCollection col = doc.GetElementsByTagName("div");
            foreach (HtmlElement element in col)
            {
                string cls = element.GetAttribute("className");
                if (String.IsNullOrEmpty(cls) || !cls.Equals("XYZ"))
                    continue;


                HtmlElementCollection childDivs = element.Children.GetElementsByName("ABC");
                foreach (HtmlElement childElement in childDivs)
                {
                    //grab links and other stuff same way
                }

            }

Also note the use of "className" instead of "class", it will get you the name of the proper class. Using just "class" will return an empty string. This is documented in MSDN - SetAttribute, but not in GetAttribute. So it causes a little bit of confusion.

Stan R.