GetElementsByTagName in Htmlagilitypack

37,118

Solution 1

If you're looking for the tag by its tagName (such as form for <form name="someForm">), then you can use:

var forms = document.DocumentNode.Descendants("form");

If you're looking for the tag by its name property (such as someForm for <form name="someForm">, then you can use:

var forms = document.DocumentNode.Descendants().Where(node => node.Name == "formName");

For the last one you could create a simple extension method:

public static class HtmlNodeExtensions
{
    public static IEnumerable<HtmlNode> GetElementsByName(this HtmlNode parent, string name)
    {
        return parent.Descendants().Where(node => node.Name == name);
    }

    public static IEnumerable<HtmlNode> GetElementsByTagName(this HtmlNode parent, string name)
    {
        return parent.Descendants(name);
    }
}

Note: You can also use SelectNodes and XPath to query your document:

var nodes = doc.DocumentNode.SelectNodes("//form//input");

Would give you all inputs on the page that are in a form tag.

var nodes = doc.DocumentNode.SelectNodes("//form[1]//input");

Would give you all the inputs of the first form on the page

Solution 2

Any node by name:

doc.DocumentNode.SelectNodes("//*[@name='name']")

Input nodes by name:

doc.DocumentNode.SelectNodes("//input[@name='name']")

Solution 3

I think you are looking for something like this

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml("....");

var inputs = doc.DocumentNode.Descendants("input")
    .Where(n => n.Attributes["name"]!=null && n.Attributes["name"].Value == "sometext")
    .ToArray();
Share:
37,118

Related videos on Youtube

Ali
Author by

Ali

Updated on July 09, 2022

Comments

  • Ali
    Ali almost 2 years

    How do I select an element for e.g. textbox if I don't know its id?

    If I know its id then I can simply write:

    HtmlAgilityPack.HtmlNode node = doc.GetElementbyId(id);
    

    But I don't know textbox's ID and I can't find GetElementsByTagName method in HtmlagilityPack which is available in webbrowser control. In web browser control I could have simply written:

    HtmlElementCollection elements = browser[i].Document.GetElementsByTagName("form");
    foreach (HtmlElement currentElement in elements)
    {
    
    }
    

    EDIT

    Here is the HTML form I am talking about

    <form id="searchform" method="get" action="/test.php">
    <input name="sometext" type="text">
    </form>
    

    Please note I don't know the ID of form. And there can be several forms on same page. The only thing I know is "sometext" and I want to get this element using just this name. So I guess I will have to parse all forms one by one and then find this name "sometext" but how do I do that?

    • shriek
      shriek about 12 years
      well, what does the html look like? there might be another way.
    • Ali
      Ali about 12 years
      @shriek I have edited my question please have a look.
    • TFD
      TFD over 11 years
  • jessehouwing
    jessehouwing about 12 years
    No need for the n.Attributes checks and all. There's a Name property. You can use .Where(n => n.Name == TheNameYoureLookingFor);
  • L.B
    L.B about 12 years
    @jessehouwing while i was preparing the answer, OP hadn't edited the question and the attribute was unknown.
  • L.B
    L.B about 12 years
    @jessehouwing No I won't think of it too much for just a few lines of html OP has shown. I just showed the way and don't think to write OP's full code.
  • jessehouwing
    jessehouwing about 12 years
    I prefer to give complete and concise answers to prevent having to answer 10 new questions.
  • L.B
    L.B about 12 years
    @jessehouwing I can not say that I am much interested in how you answer the questions. If you have something better to say than write as an answer. This is my way.