Search code examples
c#css-selectorshtml-agility-pack

HtmlAgilityPack get all elements that have an a tag nested


This is using HtmlAgilityPack . I need to get tr tags with a class of body11 that contain an a tag.I need to access with one of the cells in the row but only for the tags that have a link.have not been having much luck enter image description here

   var tablerows = table.SelectNodes("tr[@class='body11']");

       foreach(HtmlNode row in tablerows)
        {
            var cells = row.SelectNodes(".//td");

            Console.WriteLine(cells[0].InnerText);


        }

also having issue with accessing each individual cell. Currently prints out multiple cells at the same time

                          SReason:
                          Contract Ended
                                                    Pay Rate:
                          $73.58
                                                    Date Created
                                                            :
                                                        09/05/2017



                          Sup:
                          Health Carousel LLC
                                                    Bill Rate:
                          $73.58
                                                    Date Filled:
                          09/07/2017


                          City:
                          Los Angeles
                                                    OT Rate:
                          $110.37
                                                    Date Start:

                              09/03/2017

Solution

  • The selector tr.body11 > td > a should work for you. This will select any <a> node nested in a <td> nested in a <tr> that has the class "body11".

    This will select you nodes. If you need access to the or , you'll need to traverse up the node graph.