html - 如何使用xpath在html中找到第二个td

标签 html xpath html-table

我在 2 个不同的表中出现了 2 个相同的 td。

我可以使用这个获得第一个值"is":

//h:td[1][*[contains(.,'Loudspeaker')]]/../h:td[last()]/text()

但没有获得第二个值“语音 75dB/噪音 66dB/铃声 75dB”。 我试过:

//h:td[2][*[contains(.,'Loudspeaker')]]/../h:td[last()]/text()

我对 html 和 xpath 很陌生,所以请多多包涵。

我的 html 部分:

</table><table cellspacing="0">
<tr>
<th rowspan="3" scope="row">Sound</th>
<td class="ttl"><a href="glossary.php3?term=call-alerts">Alert types</a></td>
<td class="nfo">Vibration; MP3, WAV ringtones</td>
    </tr>

<tr>
<td class="ttl"><a href="glossary.php3?term=loudspeaker">Loudspeaker</a> </td>
<td class="nfo">Yes</td>
</tr>

. .

<table cellspacing="0">
<tr>
<th rowspan="5" scope="row">Tests</th>
<td class="ttl"><a href="xyz.php">Display</a></td>
<td class="nfo">
<a class="noUnd" href="http://xyz.php">Contrast ratio: Infinite (nominal) / 3.419:1     (sunlight)</a></td>
</tr><tr>

<td class="ttl"><a href="xyz.php">Loudspeaker</a></td>
<td class="nfo">
<a class="noUnd" href="http://xyz.php">Voice 75dB / Noise 66dB / Ring 75dB</a></td>
</tr><tr>

..

提前致谢。

最佳答案

这两个片段之间的唯一区别是,在第二个片段中,您的文本嵌套在 a 元素中。所以它必须是

//h:td[2][*[contains(.,'Loudspeaker')]]/../h:td[last()]/h:a/text()

(我猜你在 XPath 中使用了 h 的命名空间定义。

关于html - 如何使用xpath在html中找到第二个td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16732508/

相关文章:

Java:使用 JSoup 提取其中包含特定单词的所有链接?

html - 导航菜单项未垂直居中

css - 使用 CSS/HTML5 的横向组织

python - 尝试通过selenium python选择输入框,但每次重新加载页面时元素中的 'id'都会发生变化

java - 如何在 Java 中使用 XPATH 读取 xml

php - 数据表中基于日期的搜索功能

javascript - 表中点击时如何获取表TD标签数据?

javascript - 带有 'allow-same-origin' 标志错误的 Iframe 沙盒

javascript - Jquery 从表中的特定类获取 html

python - 按标题类型拆分 HTML div 后,如何提取我感兴趣的内容?