Find specific data in html using HtmlElement (Collection) and webbrowser

I want to find a div with the class name XYZ, then in it I want to scroll through a bunch of elements called ABC. Then grab the links (href) inside and possibly other information.

How to find a div with XYZ from webBrowser1.Document.Linksand any subtopics I want?

+3
source share
1 answer

At first you said you wanted to find a div with the class name XYZ, so why are you looking at webBrowser1.Documnet.Links? First find the Div, then go to the links inside 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
    }
}

"className" "class", . "" . MSDN - SetAttribute, GetAttribute. .

+13

Source: https://habr.com/ru/post/1713118/


All Articles