Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Why Method HtmlDocument.GetElementById matchs tag's attribute name?

HtmlDocument.GetElementById("$id") 

I want to use this method to get the element with $id, but it matches a meta tag with an attribute which has same value as $id.

HtmlDocument is like this:

<html>
    <head>
        <meta name="description" content="">
    </head>
    <body>
        <div id="description"></div>
    </body>
</html>

I tried to get tag div with id of "description":

HtmlElement elem = doc.GetElementById("description");

But I got meta instead of div. Why is the meta tag matching?

like image 515
lauriezzc Avatar asked Mar 19 '15 04:03

lauriezzc


1 Answers

Why? Here is an official reference from Microsoft: getElementById method: Returns a reference to the first object with the specified value of the ID or NAME attribute.

Solution: You should avoid name attrib in the body so you can reference tags by id if you use myHtmlDocument.Body.All[id] formula as sharique ansari mentioned.

Cheers

like image 110
Gulyás Ágyú Avatar answered Oct 15 '22 07:10

Gulyás Ágyú