2015-02-07 53 views
0

我創建了一個小腳本,它循環並刪除表中不需要的行,但表中有一行無法刪除。我怎樣才能跳過那一行然後繼續下一行呢?如何跳過表格行

這是我的腳本:

for(int i=0; i<25; i++){ 

     if(driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS)).getText().contains("Skip Me")){ 
      //what to add here to skip the "Skip Me" text???? 
     } 

     //select the item in the table 
     driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS)).click(); 

     //click the delete button 
     driver.findElement(By.xpath(".//*[@id='deleteAdv']")).click(); 

這是列的樣子。我想跳過RealMedia,然後刪除前後的所有項目。

enter image description here

HTML:

<table class="table smallInput dataTable" id="dataTableAdvertisers" "> 
<thead> 
<tr role="row" style="height: 0px;"> 
<th class="sorting_asc" tabindex="0" "></th> 
<th class="sorting" tabindex="0" "></th> 
<th class="sorting" tabindex="0" "></th> 
</tr> 
</thead> 
<tbody role="alert" aria-live="polite" aria-relevant="all"> 
<tr class="odd"> 
<td class=""> 
<a href="getadvertiserdetailsNew.do?advertiserKey=198909">RealMedia</a></td> 
<td class="">---</td> 
<td class="">---</td> 
<td class="">---</td> 
<td class="">---</td> 
</tr><tr class="even"> 
<td class=""> 
<a href="getadvertiserdetailsNew.do?advertiserKey=198910">teset2</a></td> 
<td class="">---</td> 
<td class="">---</td> 
<td class="">---</td><td class="">---</td> 
</tr><tr class="odd"> 

</tbody> 
</table> 
+0

什麼是PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS的價值? – vins 2015-02-07 17:45:17

+0

@familyGuy你想保留其中的任何一個,並刪除其餘的?或者你想保留一個特定的列表?並且,請提供該列表的'html' – Saifur 2015-02-07 17:49:51

+0

這是xpath,如果這就是你要求的?謝謝。 「.//*[@id='dataTableAdvertisers']/tbody/tr/td/a」 – familyGuy 2015-02-07 17:50:30

回答

1

嘗試以下操作: 確保有獲取列表之前一些等待(如果需要)。 此元素列表將查找該表下的所有a標記,並且for循環遍歷該集合並刪除該集合中沒有與文本匹配的任何成員RealMedia。你不應該盲目地設置迭代器的上限。這會使程序循環不必要,這是一個不好的做法。

List<WebElement> elements = driver.findElements(By.cssSelector("#dataTableAdvertisers a")); 

for (WebElement element: elements){ 
    if (!element.getText().contains("RealMedia")){ 
     //select the item in the table 
     driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS)).click(); 

     //click the delete button 
     driver.findElement(By.xpath(".//*[@id='deleteAdv']")).click(); 
    } 
} 

編輯:

By selector = By.cssSelector("#dataTableAdvertisers a"); 
List<WebElement> elements = driver.findElements(selector); 

//This just controls the loop. Iterating through the collection will return StaleElement ref exception 
for (int i = 0; i<elements.size(); i++){ 

    //Just want to delete the first item on the list 
    By xpath = By.xpath("//table[@id='dataTableAdvertisers']//a[not(.='RealMedia')]"); 

    if (driver.findElements(xpath).size()>0){ 
     WebElement element = driver.findElements(xpath).get(0); 

     element.click(); 

     //click the delete button 
     driver.findElement(By.xpath(".//*[@id='deleteAdv']")).click(); 
    } 
} 
+0

它仍然選擇了RealMedia,並在此之後出現此錯誤消息:StaleElementReferenceException:在緩存中找不到元素 - 可能頁面在查找後發生了變化 – familyGuy 2015-02-07 18:07:56

+0

因此'driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS))。點擊(); '帶你到不同的頁面? – Saifur 2015-02-07 18:15:34

+0

是的,因爲它沒有跳過列表中的RealMedia。所以,發生的情況是,當您點擊列表中的任何項目時,會將您帶到另一個頁面,重新確認您確定要永久刪除它。如果是RealMedia,它不會給出這個選項,而是說你不能刪除這個ID。目前,唯一的選擇是取消該頁面。 – familyGuy 2015-02-07 18:19:47