I have an XmlDocument containing a XHTML table. I'd like to loop through it to process the table cells one row at a time, but the code below is returning all the cells in the nested loop instead of just those for the current row:
XmlNodeList tableRows = xdoc.SelectNodes("//tr");
foreach (XmlElement tableRow in tableRows)
{
XmlNodeList tableCells = tableRow.SelectNodes("//td");
foreach (XmlElement tableCell in tableCells)
{
// this loops through all the table cells in the XmlDocument,
// instead of just the table cells in the current row
}
}
What am I doing wrong? Thanks
We can locate child nodes of web elements with Selenium webdriver. First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the children with the findElements(By. xpath()) method.
You have to select the parents first, then use a [#] predicate tag, then select all first children. //div[@class='row spacing-none']/div[1]//a[1] is the XPath you need to select the first a tag link on the whole page.
As defined in the W3 XPath 1.0 Spec, " child::node() selects all the children of the context node, whatever their node type." This means that any element, text-node, comment-node and processing-instruction node children are selected by this node-test.
Start the inner path with a "." to signal that you want to start at the current node. A starting "/" always searches from the root of the xml document, even if you specify it on a subnode.
So:
XmlNodeList tableCells = tableRow.SelectNodes(".//td");
or even
XmlNodeList tableCells = tableRow.SelectNodes("./td");
as those <td>
s probably are directly under that <tr>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With