获取属性的子字符串不起作用

Getting a substring of an attribute is not working

这是 HTML 代码:

<table id="laptop_detail" class="table">
    <tbody>
        <tr>
            <td style="padding-left:36px" class="ha">&nbsp;Camera Pixels &nbsp;</td>
            <td class="val">8 megapixel camera</td>
        </tr>
    </tbody>
</table>

如何只获取 chrome 中第一个字符“8”?到目前为止我的方法是:

$x('//*[@id="laptop_detail"]//tr/td[contains(. ,"Camera")]/following-sibling::td[1]/text()[substring(. , 0, 2)]')

不要将需要输出的函数放入谓词中,而是将其应用于节点:

substring(//*[@id="laptop_detail"]//tr/td[contains(., "Camera")]/following-sibling::td[1], 1, 1)

请注意,在 XPath 中,字符串中的字符从 1 开始编号,而不是 0。

此外,您不需要指定 text()substring 知道它应该对字符串进行操作。

顺便说一句,如果百万像素数是10,你真的想得到1吗?也许

substring-before(..., ' ')

效果会更好吗?