2014-03-26 230 views
1

有沒有辦法在Selenium Webdriver中進一步點擊兄弟姐妹?例如,這裏的HTML:Selenium兄弟姐妹

<tr class="rich-table-row xx-datalist-even" onmouseover="Element.addClassName(this, 'xx-datalist-mouseover')" onmouseout="Element.removeClassName(this, 'xx-datalist-mouseover')"> 
    <td class="rich-table-cell xx-datalist " id="userTableForm01:usersTableData:264:j_id102" style="text-align:center; width:20px;"> 
     <img src="/static/images/graphics/status1.gif" title="Disabled" /> 
    </td> 
    <td id="userTableForm01:usersTableData:264:col1" class="rich-table-cell xx-datalist"> 
     <span id="userTableForm01:usersTableData:264:author_id">2377</span> 
    </td> 
    <td id="userTableForm01:usersTableData:264:col2" class="rich-table-cell xx-datalist">seleniumtest2312</td> 
    <td id="userTableForm01:usersTableData:264:col3" class="rich-table-cell xx-datalist"> 
     <span style="margin-right:3px">Test2312</span> 
     Selenium 
    </td> 
    <td class="rich-table-cell xx-datalist " id="userTableForm01:usersTableData:264:col4" style="text-align:center;"> 
     <input type="checkbox" name="userTableForm01:usersTableData:264:j_id111" onclick="A4J.AJAX.Submit('userTableForm01',event,{'similarityGroupingId':'userTableForm01:usersTableData:264:j_id112','parameters':{'userTableForm01:usersTableData:264:j_id112':'userTableForm01:usersTableData:264:j_id112'} })" /> 
    </td> 
    <td id="userTableForm01:usersTableData:264:col6" class="rich-table-cell xx-datalist"> 
     <a id="userTableForm01:usersTableData:264:enable_link" href="#" style="margin-right:5px" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('userTableForm01'),{'userTableForm01:usersTableData:264:enable_link':'userTableForm01:usersTableData:264:enable_link'},'');}return false"> 
      <span id="userTableForm01:usersTableData:264:enable_link_text" class="xx-datalist">Enable</span> 
     </a> 
     <a id="userTableForm01:usersTableData:264:edit_link" href="#" style="margin-right:5px" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('userTableForm01'),{'userTableForm01:usersTableData:264:edit_link':'userTableForm01:usersTableData:264:edit_link'},'');}return false"> 
      <span id="userTableForm01:usersTableData:264:edit_link_text" class="xx-datalist">Edit</span> 
     </a> 
     <a id="userTableForm01:usersTableData:264:remove_link" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('userTableForm01'),{'userTableForm01:usersTableData:264:remove_link':'userTableForm01:usersTableData:264:remove_link'},'');}return false"> 
      <span id="userTableForm01:usersTableData:264:remove_link_text" class="xx-datalist">Delete</span> 
     </a> 
    </td> 
</tr> 

現在,這是一個更大的表比所示,所以有很多TR與更多的用戶。這些ID都是自動生成的,每次都會有所不同,我試圖點擊'Enable'鏈接,儘管我可以使用的唯一同一行唯一的東西是用戶名seleniumtest2312,它給我帶來了:

driver.findElement(By.xpath("//td[text()='seleniumtest2312']/following-sibling::td[3]/span[text()='Enable']")).click(); 

但它只是不會工作(沒有這樣的元素)。

如果有更好的方法來解決這個問題,我會很樂意嘗試。

回答

0

您的XPath的最後一部分是有點過:

following-sibling::td[3]/span[text()='Enable'] 

因爲<span>你要找的是不是<td>直接孩子。你可以試試這個XPath,而不是:

//td[.='seleniumtest2312']/following-sibling::td[3]/a/span[.='Enable'] 
+0

...我是一個恥辱,autoamted測試,但非常感謝發現錯誤:-) – Odecif

+0

沒有問題,第二雙眼睛是有用的,有時 – har07