验证xpath中的多个条件

时间:2014-01-04 19:26:51

标签: html selenium xpath parent-child siblings

我得到了以下HTML表格,我需要找到包含tr span的{​​{1}}的ID,其值为Action2Server1以及Tag2

<table width="100%" cellspacing="0" cellpadding="0" border="0" style="table-layout:fixed;" id="rtTkFrm:recTkTabId" class="dr-table rich-table ">
    <colgroup span="8"></colgroup>
    <tbody id="rtTkFrm:recTkTabId:tb">
        <tr class="dr-subtable-row rich-subtable-row dr-subtable-firstrow rich-subtable-firstrow 0">
            <td width="2%" style="padding-left:17px;" id="rtTkFrm:recTkTabId:0:innertable:0:j_id31" class="dr-subtable-cell rich-subtable-cell ">
                <label class="CollapseRow" onclick="noOfRows(this);" id="rtTkFrm:recTkTabId:0:innertable:0:expand"></label>
            </td>
            <td style="width:20%" id="rtTkFrm:recTkTabId:0:innertable:0:TkDesc" class="dr-subtable-cell rich-subtable-cell adjacent TkDescC">
                <span title="Action1" class="adjacentSpan" id="rtTkFrm:recTkTabId:0:innertable:0:cell-">Action1</span>
            </td>
            <td style="width:22%" id="rtTkFrm:recTkTabId:0:innertable:0:tgt" class="dr-subtable-cell rich-subtable-cell adjacent tgtC">
                <span title="Server1" class="adjacentSpan">Sever1</span>
            </td>
            <td style="width:16%" id="rtTkFrm:recTkTabId:0:innertable:0:tgtDep" class="dr-subtable-cell rich-subtable-cell adjacent tgtDepC">
                <span title="Tag1" class="adjacentSpan">Tag1</span>
            </td>
        </tr>
        <tr class="dr-subtable-row rich-subtable-row dr-subtable-firstrow rich-subtable-firstrow 0">
            <td width="2%" style="padding-left:17px;" id="rtTkFrm:recTkTabId:0:innertable:0:j_id31" class="dr-subtable-cell rich-subtable-cell ">
                <label class="CollapseRow" onclick="noOfRows(this);" id="rtTkFrm:recTkTabId:0:innertable:0:expand"></label>
            </td>
            <td style="width:20%" id="rtTkFrm:recTkTabId:0:innertable:0:TkDesc" class="dr-subtable-cell rich-subtable-cell adjacent TkDescC">
                <span title="Action2" class="adjacentSpan" id="rtTkFrm:recTkTabId:0:innertable:0:cell-">Action2</span>
            </td>
            <td style="width:22%" id="rtTkFrm:recTkTabId:0:innertable:0:tgt" class="dr-subtable-cell rich-subtable-cell adjacent tgtC">
                <span title="Server1" class="adjacentSpan">Server1</span>
            </td>
            <td style="width:16%" id="rtTkFrm:recTkTabId:0:innertable:0:tgtDep" class="dr-subtable-cell rich-subtable-cell adjacent tgtDepC">
                <span title="Tag2" class="adjacentSpan">Tag2</span>
            </td>
        </tr>
                <tr class="dr-subtable-row rich-subtable-row dr-subtable-firstrow rich-subtable-firstrow 0">
            <td width="2%" style="padding-left:17px;" id="rtTkFrm:recTkTabId:0:innertable:0:j_id31" class="dr-subtable-cell rich-subtable-cell ">
                <label class="CollapseRow" onclick="noOfRows(this);" id="rtTkFrm:recTkTabId:0:innertable:0:expand"></label>
            </td>
            <td style="width:20%" id="rtTkFrm:recTkTabId:0:innertable:0:TkDesc" class="dr-subtable-cell rich-subtable-cell adjacent TkDescC">
                <span title="Action1" class="adjacentSpan" id="rtTkFrm:recTkTabId:0:innertable:0:cell-">Action1</span>
            </td>
            <td style="width:22%" id="rtTkFrm:recTkTabId:0:innertable:0:tgt" class="dr-subtable-cell rich-subtable-cell adjacent tgtC">
                <span title="Server2" class="adjacentSpan">Server2</span>
            </td>
            <td style="width:16%" id="rtTkFrm:recTkTabId:0:innertable:0:tgtDep" class="dr-subtable-cell rich-subtable-cell adjacent tgtDepC">
                <span title="Tag2" class="adjacentSpan">Tag2</span>
            </td>
        </tr>
    </tbody>
</table>

我尝试了以下但是没有按预期工作

//id('recentTaskFrm:recTaskTabId')/tbody/tr/td[descendant::span[@title='Action1' and @title='Server2' and @title='Tag2']]/@id

2 个答案:

答案 0 :(得分:0)

试试这个://id('recentTaskFrm:recTaskTabId')/tbody/tr/td[descendant::span[@title='Action1'] and descendant::span[@title='Server2'] and descendant::span[@title='Tag2']]/@id

答案 1 :(得分:0)

有两个问题:

  1. 您查询了错误的@id属性(rtTkFrm:recTkTabId,而不是recentTaskFrm:recTaskTabId)。
  2. 您需要搜索多个后代,每个后代各有一个谓词。您的查询尝试同时找到标题为“Action1”,“Server2”和“Tag2”的span元素。
  3. 您正在查询表格单元格的@id属性,而不是行。
  4. 尝试此查询:

    //table[@id='rtTkFrm:recTkTabId']/tbody/tr[
      td//span[@title="Action2"]
      and td//span[@title="Server1"]
      and td//span[@title="Tag2"]
    ]