XPath select's in HTMLAgilityPack don't work as expected

限于喜欢 提交于 2019-12-05 09:22:38

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..."

[CodePlex discussion : No child nodes for FORM objects ]

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!