xpath selector tr given list of data

时间:2016-10-20 13:23:53

标签: selenium xpath css-selectors cucumber

I am automating a web system using selenium webdriver, in multiple scenarios I need to assert that given data in in one line (tr) of the web page.

For example, I need to assert that there is in the system a line (tr) with these data: Data 1, Data 2, Data 3, Data 4

And my page is structured like this:

<tr>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Data 3</td>
    <td><span class='something'>Data 4</span></td>
<tr>

I wrote this xpath hoping it would return the table row:

.//tr[td[text()='Data 1']]/parent::tr/td[text()='Data 2']/parent::tr/td[text()='Data 3']/parent::tr/td[text()='Data 4']

It works until Data 3 but in Data 4 does not work

3 个答案:

答案 0 :(得分:0)

它不起作用,因为第4 <span>中嵌入了<td>个元素。

你应该试试

.//tr[td[text()='Data 1']]/.../parent::tr/td[.='Data 4']

.//tr[td[text()='Data 1']]/.../parent::tr/td[span[text()='Data 4']]

答案 1 :(得分:0)

  

我想要一个在我的td

中的任何级别匹配的通用解决方案

您应该尝试using . instead of text(),这将是一个通用解决方案,如下所示: -

.//tr[td[.='Data 1'] and td[.='Data 2'] and td[.='Data 3'] and td[.='Data 4']]

或者,如果您想要前导和尾随空格,请尝试使用xpath的{​​{3}}函数,如下所示: -

.//tr[td[normalize-space()='Data 1'] and td[normalize-space()='Data 2'] and td[normalize-space()='Data 3'] and td[normalize-space()='Data 4']]

答案 2 :(得分:0)

尝试使用此XPath,我认为它适用于您的情况:

.//tr[.//*[.='Data 1'] and [.='Data 2'] and [.='Data3'] or [.='Data 4']]

它将搜索包含这些文本的子节点。