2017-02-21 191 views
-1

table image需要刪除

硒網絡驅動程序代碼,我想這一個:

driver.findElement(By.xpath(".//a[contains(.,'terw')]")).findElement(By.xpath(".//img[@id='delete_fully_img'])")).click(); 

,但其沒有工作,我的代碼:

<table id="Table_1" class="dataTable" aria-describedby="Table_1_info" style="width: 100px;"> 
<thead> 
    <tr class="header" role="row"> 
     <td class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 213px;">Point Name</td> 
     <td class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 149px;">Description</td> 
     <td class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 150px;">Date Added</td> 
     <td class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 46px;"/> 
    </tr> 
</thead> 
<tbody role="alert" aria-live="polite" aria-relevant="all"> 
    <tr class="odd"> 
     <td class=" "> 
      <a id="attachment_title_382" slider_number="0" onclick="AddPoints('edit', '382', '');" href="javascript:void(0);">terw</a> 
     </td> 
     <td class=" "/> 
     <td class=" ">02/21/2017</td> 
     <td class=" "> 
      <img id="delete_fully_img" onclick="deletePoint('', '382', '382', '0');" title="Delete Point" style="height:17px;width:17px;" src="http //close_icon.png"/> 
     </td> 
    </tr> 
    <tr class="even"> 
     <td class=" "> 
      <a id="attachment_title_301" slider_number="0" onclick="AddPoints('edit', '301', '1');" href="javascript:void(0);">1</a> 
     </td> 
     <td class=" "/> 
     <td class=" ">02/15/2017</td> 
     <td class=" "> 
      <img id="delete_fully_img_1" onclick="deletePoint('', '301', '301', '0');" title="Delete Point" style="height:17px;width:17px;" src="http //close_icon.png"/"/> 
     </td> 
    </tr> 
</tbody> 

+1

問題不明確,請添加其他細節或刪除的問題,如果它已經被問錯。 – user3251882

+0

錯誤?這是否真的發生?我只是想知道是否有人過來這個網站,並且「哎呀,我只是把這一切都錯了。」 – RSon1234

+0

閱讀文章。 http://stackoverflow.com/help/how-to-ask –

回答

0

嘗試以下任一代碼。

說明:使用tr標籤的class屬性,儘量使用text方法首先要找到像02/21/2017元素的文本。然後使用following關鍵字繼續使用img標記。

driver.findElement(By.xpath("//tr[@class='odd']/td[contains(text(), '02/21/2017')]/..//following::img[@id='delete_fully_img']")).click(); 

OR

driver.findElement(By.xpath("//tr[@class='odd']/..//following::img[@id='delete_fully_img']")).click(); 

OR

driver.findElement(By.xpath("//img[@id='delete_fully_img']")).click(); 

OR

driver.findElement(By.xpath("//tr[@class='odd']//img[@id='delete_fully_img']")).click(); 

OR

driver.findElement(By.xpath("//tr[@class='odd']//child::img[@id='delete_fully_img']")).click(); 
+0

感謝它的工作,在這個driver.findElement(By.xpath(「// tr [@ class ='odd']/td [contains(text() ,'02/21/2017')] /..// following :: img [@ id ='delete_fully_img']「))。click();有可能把名稱而不是日期? – user3847594

+0

不,你不能放置名稱,因爲你的'td'標籤在這裏只包含'date'。請參閱html'​​02/21/2017'如果問題已解決,請將此答案標記爲已接受。 –

+0

我已經創建了新的'xpath'。但它的理解更爲複雜。試試這個xpath:''driver.findElement(By.xpath(「// tr [@ class ='odd']/td/a [contains(text(),'terw')] /..// following -sibling :: td [contains(text(),'02/21/2017')] /..// following-sibling :: td/img [@ id ='delete_fully_img']「))。click();' –

0

使用XPath: // tr [包含(@class,「even」)] // td/img [contains(@id,「delete_fully_img_1」)和包含(@title,「刪除點」)] 希望它適合你。

0

試試這個如下: -

driver.findElement(By.xpath("//img[@id='delete_fully_img']")) 

OR

driver.findElement(By.xpath("//tr[@class='odd']//img[@id='delete_fully_img']")) 

OR

driver.findElement(By.xpath("//img[@id='delete_fully_img']/ancestor::tr[@class='odd']")) 
+0

你好@Paras,你的兩個xpath都不行,因爲你的目標是'a'標籤。但在id屬性包含''img'標籤的'html'裏面。 –

+0

ohh我的壞..更新 – Paras

+0

你最後的xpath在這裏將不起作用,因爲你的xpath會定位整行,所以在這裏不是使用'ancestor'關鍵字來使用'child'關鍵字,所以它只會定位到特定的刪除圖標按鈕。用'child'關鍵字查看我的最後一個答案。 –