Xpath在C#中未返回正确的结果

时间:2019-01-30 08:47:52

标签: c# xpath

我正在尝试从C#.NET应用程序中运行XPath查询,但结果为空。相同的查询使用Chrome浏览器中的扩展程序XPath Helper返回结果。请告知我所缺少的以便提取数据。

string query = "/html/body/div[3]/div/div[2]/div[1]/ul[2]/li/@data-index";
XPathResult xpathResult = geckoWebBrowser1.Document.EvaluateXPath(query);

var foundNodes = xpathResult.GetNodes();
foreach (var node in foundNodes)
{
    var x = node.TextContent; // get text text contained by this node (including children)
    GeckoHtmlElement element = node as GeckoHtmlElement; //cast to access.. inner/outerHtml
    string inner = element.InnerHtml;
    string outer = element.OuterHtml;
    //iterate child nodes
    foreach (var child in node.ChildNodes)
    {
        txtResults.AppendText(child.NodeValue + System.Environment.NewLine);

    }
}

下面的HTML代码。我有兴趣检索以下内容。前四个我没有问题:

"/html/body/div[3]/div/div[2]/div[1]/h2/p[1]"

"/html/body/div[3]/div/div[2]/div[1]/h2/p[2]"

"/html/body/div[3]/div/div[2]/div[1]/ul[2]/li/div/p[1]"

"/html/body/div[3]/div/div[2]/div[1]/ul[2]/li/div/p[2]"

"/html/body/div[3]/div/div[2]/div[1]/ul[2]/li/@data-index"

"/html/body/div[3]/div/div[2]/div[1]/ul[2]/li/@data-value"

Elements from Chrome

XPath Helper Query result in Chrome

无法粘贴代码。检测为垃圾邮件。链接到这里:https://pastebin.com/NUPbgRWR

0 个答案:

没有答案