根据TH和TR选择输入字段

时间:2015-09-21 13:32:08

标签: xpath

我有一张TH的下一张桌子:

<table cellspacing="1" border="1" id="FinancialsGrid">
    <thead>
        <tr>
            <th>Product</th>
            <th>Slice</th>
            <th>Units</th>
            <th>Accrual Rate</th>
            <th>Trend Factor</th>
            <th>Base Units</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td rowspan="3">Lorem Ipsum</td>
            <td>Previous</td>
            <td>6,866</td>
            <td>0.00 %</td>
            <td>0.00 %</td>
            <td>6,866</td>
        </tr>
        <tr>
            <td>Current</td>
            <td>6,866</td>
            <td>0.00 %</td>
            <td>0.00 %</td>
            <td>6,866</td>
        </tr>
        <tr>
            <td>Proposed</td>
            <td>6,866</td>
            <td><input type="text" style="width:60px;" value="0.00 %"></td>
            <td style="width:60px;"><input type="text" style="width:60px;" value="0.00 %"></td>
            <td style="width:60px;"><input type="text" style="width:90px;" value="6,866"></td>
        </tr>
    </tbody>
</table>

我需要根据例如选择输入字段的xpath。 “趋势因子”栏目。

我写的我的变体不起作用:

//table[@id='FinancialsGrid']/tbody/tr/td/input[count(//table/thead/tr/th[.='Trend Factor'])]

表视图: enter image description here

1 个答案:

答案 0 :(得分:0)

这是XPath

(//table[@id='FinancialsGrid']//input)[count(//tr/th)-index-of(//tr/th, //tr/th[text()='Trend Factor']) + 1]

或没有 index-of()功能的另一个(对于Firebug):

(//table[@id='FinancialsGrid']//input)[count(//tr/th)-count(//tr/th[text()='Trend Factor']/preceding-sibling::*)]

将获取所需的输入节点,其中:

count(//tr/th) is count of columns
index-of(//tr/th, //tr/th[text()='Trend Factor']) - current number of column
(//table[@id='FinancialsGrid']//input) - sequence of input nodes.

因此我们从序列中计算输入节点和grubbing节点的位置。 enter image description here