[英]Getting XPATH from table in webpage
I have the following xpath: /html/body/div/div/div/div[2]/div/div/div/div[3]/div/table/tbody/tr/td[2]/a
我有以下xpath:
/html/body/div/div/div/div[2]/div/div/div/div[3]/div/table/tbody/tr/td[2]/a
from: 从:
<div id="search-result-block">
<div class="">
<table class="search-result" width="100%">
<tbody>
<tr>
<td class="result-num">
<td class="result-header" colspan="2">
<a class="srtitle" href="http://books.ioba.org/books/4359746.html">1001 Designs for Whittling and Woodcarving</a>
<br>
<div class="srauthor">Tangerman, Elmer John </div>
</td>
</tr>
<tr>
</tbody>
</table>
and I need to get the inner html of that xpath, however my application is giving me a null exception (i guess it is finding that the path is wrong or something) 并且我需要获取该xpath的内部html,但是我的应用程序给了我一个空异常(我想它发现路径错误或其他原因)
Here is my current code: 这是我当前的代码:
HtmlAgilityPack.HtmlNode rootNode = htmlDoc.DocumentNode;
titleBook = rootNode.SelectSingleNode(@".//table/tbody/tr/td[2]/a").InnerHtml.ToString();
I tried using @"//table[contains(@class, 'result-header')]/a"
instead but still did not work. 我尝试使用
@"//table[contains(@class, 'result-header')]/a"
代替,但仍然无法正常工作。 What am I doing wrong? 我究竟做错了什么?
All in all, I believe the easiest way to select your node is 总而言之,我认为选择节点的最简单方法是
//a[@class='srtitle']
But your HTML is really messy... Missing some matching tags, for example, which will render the behavior non-predictable. 但是您的HTML确实很乱。例如,缺少一些匹配的标记,这将使行为无法预测。
Your first path doesn't work because the two td
seem to be actually nested, not siblings: it should be @"//table/tbody/tr/td/td/a"
. 您的第一个路径无效,因为两个
td
似乎实际上是嵌套的,而不是同级的:它应该是@"//table/tbody/tr/td/td/a"
。
The second xpath could be simplified and fixed with @"//td[@class="result-header']/a
, or if you want @"//td[contains(@class, 'result-header')]/a"
. 第二个xpath可以简化并用
@"//td[@class="result-header']/a
修复,或者如果您想要@"//td[contains(@class, 'result-header')]/a"
。
I think you already have your solution, but here's a LINQ-esq library for generating xpath expressions for similar situations. 我认为您已经有了解决方案,但是这里有一个LINQ-esq库,用于为类似情况生成xpath表达式。
http://www.unit-testing.net/CurrentArticle/How-to-Create-Xpath-From-Lambda-Expressions.html http://www.unit-testing.net/CurrentArticle/How-to-Create-Xpath-From-Lambda-Expressions.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.