Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
370 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...