2015-02-07 77 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