0

I have created a small script, where it loops and deletes the unwanted rows in the table, but there is one row in the table that can not be removed. How can I skip that row and move on to the next one?

This is my script:

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();

This is what the column looks like. I want to skip RealMedia, and then remove all items before and after.

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>
familyGuy
  • 425
  • 2
  • 5
  • 22
  • What is the value of PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS? – vins Feb 07 '15 at 17:45
  • @familyGuy do you want to keep any of them and delete the rest? or you want to keep a specific one from that list? And, please provide the `html` of that list – Saifur Feb 07 '15 at 17:49
  • this is the xpath, if that's what you are asking? Thanks. ".//*[@id='dataTableAdvertisers']/tbody/tr/td/a" – familyGuy Feb 07 '15 at 17:50
  • @Saifur Yes, I want to keep only RealMedia, and delete rest of them. – familyGuy Feb 07 '15 at 17:56

1 Answers1

1

Try the following: Make sure there are some wait(if needed) before fetching the list. This elements list will find all of the a tags under that table and for loop iterate through the collection and delete any member of the collection that does not have a text matching RealMedia. You shouldn't be setting the upper limit of the iterator blindly. That will keep the program looping unnecessarily and that's a bad practice.

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();
    }
}

EDIT:

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();
    }
}
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • It still selcts the RealMedia, and after that this error message: StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up – familyGuy Feb 07 '15 at 18:07
  • So `driver.findElement(By.xpath(PvtConstants.READ_ADVERTISRERS_ADVERTISER_IDS)).click(); ` takes you to a different page? – Saifur Feb 07 '15 at 18:15
  • Yes, because it is NOT skipping the RealMedia in the list. So, what happens is, when you click on any item in the list, it takes you to a different page, where it re-confirms that are you sure you want to permanently delete it. In case of RealMedia, it does not give this option, rather it says you can not delete this id. At this time, the only option is to cancel out of that page. – familyGuy Feb 07 '15 at 18:19
  • And, when you delete(not real media) it takes you back to the previous page or you need press any other button to go back to the old page? – Saifur Feb 07 '15 at 18:20
  • Yes...that's exactly what it does...takes me back without clicking any other buttons – familyGuy Feb 07 '15 at 18:25
  • Sorry, same results, not skipping RealMedia. It deleted an id prior to it, but test stopped/passed after that. – familyGuy Feb 07 '15 at 18:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70481/discussion-between-saifur-and-familyguy). – Saifur Feb 07 '15 at 18:35