rfk12
rfk12

Reputation: 21

Get element by class name via browser C#

I have:

<h2 class="entry-title" itemprop="headline">
<a href="http://www.printesaurbana.ro/2015/10/idei-despre-un-start-bun-in-blogging.html" Idei despre un start bun în blogging </a>
</h2>

I want get href using class name, I try:

if (webBrowser1.Document != null)
                {
                    var links = webBrowser1.Document.GetElementsByTagName("a");

                    foreach (HtmlElement link in links)
                    {
                        if (link.GetAttribute("class") == "entry-title")
                        {
                            MessageBox.Show("Here");

                        }
                    }
                }

But didn't work. How solve this?

Upvotes: 1

Views: 3219

Answers (1)

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

You should use link.GetAtribute("className"). Besides, it is the h2 tag in your html document that has the entry-title class. Corrected code:

if (webBrowser1.Document != null)
            {
                var links = webBrowser1.Document.GetElementsByTagName("h2");

                foreach (HtmlElement link in links)
                {
                    if (link.GetAttribute("className") == "entry-title")
                    {
                        MessageBox.Show("Here");

                    }
                }
            }

Upvotes: 1

Related Questions