The unexpected behavior in your test occur because the html contains <form>
element. Here is related discussion :
Ariman : "I've found that after parsing any node does not have any child nodes. All nodes that should be inside the form (, , etc.) are created as it's siblings rather then children.
VikciaR : "In Html specification form tag can overlap, so Htmlagilitypack handle this node a little different..."
And as suggested by VikciaR there, try to modify your test code initialization like this :
private static HtmlNode GetHtmlDocumentNode()
{
var document = new HtmlDocument();
document.LoadHtml(html);
//execute this line once
HtmlNode.ElementsFlags.Remove("form");
return document.DocumentNode;
}
Side note: inputQuery
value in test method TwoSingleLevelXpathsTest()
should be .//input
. Notice the dot (.
) at the beginning to indicate that this query is relative to current node. Otherwise it will search from the root, ignoring the former formQuery
(without the dot, you can change formQuery
to anything as long as it doesn't return null, the inputQuery
will always return the same result).