如何在以下两个html块中找到xpath?

时间:2017-10-04 09:54:27

标签: xpath selenium-webdriver

如何在以下2个html块中找到xpath?两个html代码都相同,但只有ID不同。 Id是动态的,很难找到xpath:

<div id="discoverytree-body" class="x-panel-body x-grid-body x-panel-body-default-framed x-panel-body-default-framed x-layout-fit" style="padding: 0px; border-top-width: 1px; width: 264px; left: 0px; height: 425px; top: 48px;">
   <div id="treeview-1015" class="x-grid-view x-fit-item x-grid-view-default" tabindex="-1" style="margin: 0px; width: 262px; height: 423px;">
      <table class="x-grid-table x-grid-table-resizer" style="width:10000px;" cellspacing="0" cellpadding="0" border="0">
         <tbody>
            <tr class="x-grid-header-row">
               <th class="x-grid-col-resizer-treecolumn-1014" style="width: 10000px; height: 0px;"/>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-expanded">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner" style="text-align: left; ;">
                     <img class="x-tree-elbow-end-plus x-tree-expander" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-parent iconDiscovery" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Discovery
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div id="ext-gen1349" class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Common Settings
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div id="ext-gen1345" class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Manage Components
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-expanded x-grid-row-over">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-plus x-tree-expander" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-parent iconManagerDiscovery" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Manager Discovery
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-expanded">
               <td id="ext-gen1342" class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-plus x-tree-expander" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-parent iconSettings" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Settings
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf x-grid-row-selected x-grid-row-focused">
               <td id="ext-gen1384" class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div id="ext-gen1340" class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     General
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td id="ext-gen1380" class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1336" class="x-tree-elbow" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Virtual Platforms
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1376" class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1332" class="x-tree-elbow" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Public Clouds
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div id="ext-gen1243" class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1372" class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1239" class="x-tree-elbow" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Citrix NetScalers
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div id="ext-gen1247" class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1368" class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1325" class="x-tree-elbow" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     vCloud Directors
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div id="ext-gen1251" class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img id="ext-gen1321" class="x-tree-elbow-end" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Storage Discovery
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-expanded">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-end-plus x-tree-expander" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-parent iconActions" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Actions
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
            <tr class="x-grid-row x-grid-tree-node-leaf">
            <tr class="x-grid-row x-grid-tree-node-leaf">
            <tr class="x-grid-row x-grid-tree-node-expanded">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-end-plus x-tree-expander" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-parent iconAgentDiscovery" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Agent Discovery
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-expanded">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-plus x-tree-expander" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-parent iconSettings" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Settings
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-line" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-end" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     General
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-expanded">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-end-plus x-tree-expander" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-parent iconActions" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Actions
                  </div>
               </td>
            </tr>
            <tr class="x-grid-row x-grid-tree-node-leaf">
               <td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1014 x-grid-cell-first">
                  <div class="x-grid-cell-inner " style="text-align: left; ;">
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-empty" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-elbow-end" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     <img class="x-tree-icon x-tree-icon-leaf iconOU" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="/>
                     Enable/Disable
                  </div>
               </td>
            </tr>
         </tbody>
      </table>
   </div>
</div>
</div>

以上HTML是整页代码,在这里我必须选择Discovery - &gt;管理器发现,操作 - >启用/禁用

Xpath -

.//*[contains(text(), 'Manager Discovery')] //following::td[9]

其中td[9]表示操作 - >启用/禁用,并且UI中有1个匹配的节点..但在代码中,未找到此类元素异常。尝试使用try catch块,JavaScript,隐式和显式等待,webdriver等待

4 个答案:

答案 0 :(得分:2)

您想选择什么? divtd都包含ext-gen个ID,因此您不清楚所需的确切元素。最好指明您正在搜索divtd是否更容易将来支持(或使用classes代替ids,因为它们在这些元素上有所不同)

此处您还有很多类似的ID,因此最好使用starts-with()代替contains()

(//div[starts-with(@id,'ext-gen')][contains(.,'Enable/Disable')])[1]

(//div[starts-with(@id,'ext-gen')][contains(.,'Enable/Disable')])[2]

div could be replaced with td

一些解释

(locator)[n] - 通过此定位器搜索第n个元素among all found elements

locator[n] - 搜索nth元素by it's position in node.

其中:

  

n = position()=n的快捷方式,表示位置编号。可能   替换为任何整数1,2,..,n或任何返回的函数   整数

     

locator =任何元素定位器,例如//*[@id='someID']

例如:

(//*[@class='super'])[1] - 在页面上的所有元素中找到第一个带有类super的元素 //*[@class='super'][1] - 查找所有具有类super的元素,这些元素在其节点内具有位置1

所以对于html:

<tr>
    <td class="cell">1</td>
    <td class="cell">2</td>
</tr>
<tr>
    <td class="cell">3</td>
    <td class="cell">4</td>
</tr>

在所有示例中:(定位器)[n]

(//td[@class='cell'])[2] - 返回所有的第二个元素,因此它的值为'2'

的单元格

(//td[@class='cell'])[3] - 返回所有内容中的第3个元素,因此其值为'3'

的单元格

按节点中的位置: locator [n]

//td[@class='cell'][2] - 返回其中第二个单元格的节点,因此它将返回单元格'2' and '4'

//td[@class='cell'][3] - 不返回任何内容,因为任何节点中都没有第三个单元格

以下是评论中澄清后的XPATH:

//tr[contains(.,'Manager Discovery')]/following-sibling::tr[contains(.,'Enable/Disable')]
OR:
//tr[contains(.,'Manager Discovery')]/following-sibling::tr[16]

答案 1 :(得分:0)

您可以索引如下

(.//*[contains(@id,'ext-gen')][contains(text(), 'Enable/Disable')])[1]

OR

(.//*[contains(@id,'ext-gen')][contains(text(), 'Enable/Disable')])[2]

答案 2 :(得分:0)

您可以使用以下xpath:

对于第一个元素

//*[contains(@id,'ext-gen')][contains(., 'Enable/Disable')][1]
//*[contains(@class,'x-grid-cell-inner')][contains(., 'Enable/Disable')][1]
//div[contains(@class,'x-grid-cell-inner')][contains(., 'Enable/Disable')][1]

第二个元素

//*[contains(@id,'ext-gen')][contains(., 'Enable/Disable')][2]
//*[contains(@class,'x-grid-cell-inner')][contains(., 'Enable/Disable')][2]
//div[contains(@class,'x-grid-cell-inner')][contains(., 'Enable/Disable')][2]

答案 3 :(得分:0)

我曾经历过同样的情况一次。要单击这两个按钮,您必须使用“Follow-sibling”关键字。意味着,你必须在父母和孩子之间建立关系,以便有独特的关系。提供的html代码不足以建立关系。