C# webBrowser1 How to get element by class?

Viewed 601

Im trying to get Steam name from this page "https://steamcommunity.com/id/giogongadzee" via class name. here is html code:

<div class="profile_header_bg">

<div class="profile_header_bg_texture">

    <div class="profile_header">

        <div class="profile_header_content">

                            <div class="profile_header_centered_persona">
                <div class="persona_name" style="font-size: 24px;">
                    <span class="actual_persona_name">Ghongha</span>  <--- i want to get "Ghongha" in MessageBox.Show();

here is what i tried to do but not working...

private void GetInfo_Click(object sender, EventArgs e)
    {
        var links = webBrowser1.Document.GetElementsByTagName("a");
        foreach (HtmlElement link in links)
        {
            if (link.GetAttribute("className") == "actual_persona_name")
            {
                MessageBox.Show(link.InnerText);
            }
            else
            {
                MessageBox.Show("0");
            }
        }
    }

Help :/

2 Answers

this is an extension method :

  public static List<System.Windows.Forms.HtmlElement> GetElementsByClassName(this System.Windows.Forms.WebBrowser browser, string TagName, string Classname)
    {
        var list = new List<System.Windows.Forms.HtmlElement>();
        var elements = browser.Document.GetElementsByTagName(TagName);
        foreach (System.Windows.Forms.HtmlElement link in elements)
            if (link.GetAttribute("className") == Classname)
                list.Add(link);

        return list;
    }
Related