1

I am working on a test case and need to find text within a table. The only thing to key off of is the label in the previous column. The keys are Next Trckng/Dschrg, Next Full, Next Qtrly, Next Mdcr. I would like to create an xpath expression that will find the Text 1, Text 2, Text 3, and Text 4 based on the key. Since all the keys have the word Next in them, I have mocked this up to find all four of them at once.

//td[preceding-sibling::td[contains(descendant::text(),'Next')]]/a

The third one is not found because it does not have an 'a' element, which is fine. the problem comes in the very first td. It has a span in it, unlike the others. The span is on a second physical line from the td. It appears that the CRLF is preventing FirePath from finding the first td, when I put the span on the same line as the td, it is found. The problem is that I cannot change the actual page, this is a test case.

Is this a FireBug issue or is this actually resulting in two text elements in the DOM? How do I tweak the xpath to find all four nodes?

Here is the HTML:

<table border=1>
    <tbody>
        <tr>
            <td>
                <span id="xxx"><a><img></a></span>&nbsp;&nbsp;&nbsp;
                Next Trckng/Dschrg:</td>
            <td><a>Text 1</a></td>
            <td>Next Full:</td>
            <td><a>Text 2</a></td>
            <td>Next Qtrly:</td>
            <td>&nbsp;<!-- Text 3 --></td>
            <td>Next Mdcr:</td>
            <td><a>Text 4</a></td>
            <td>Change Of Therapy:</td>
        </tr>
    </tbody>
</table>
4

1 回答 1

1

问题在于表达式contains(descendant::text(),'Next')。该contains函数接受两个字符串作为参数。由于您将节点集作为第一个参数传递,因此它被转换为字符串。转换通过调用string节点集上的函数来进行,该函数根据规范返回文档顺序中第一个节点的字符串值。在您的情况下,这将是元素的第一个文本子td元素。对于第一个td元素,这是一个仅包含空格的文本节点。

解决方案很简单:将当前td元素传递给contains函数:

contains(., 'Next')

此单个节点的字符串值将包含所有文本节点后代的字符串值的串联

于 2013-06-05T23:29:22.793 回答