5

I have written below code for Checking list Web Elements, but below code is running but for only 1st item its no looping to end of loop.

List <WebElement> listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));

for (int i=1; i<=listofItems.size(); i++)
{
   listofItems.get(i).click();
   wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
   System.out.println(i);
   System.out.println("pass");
   wd.navigate().back();
}
3
  • 1
    What is the error? StaleElement refrence exception?
    – Saifur
    May 12, 2015 at 13:19
  • @Saifur: Error is like, org.openqa.selenium.StaleElementReferenceException: Element not found in the cache...
    – Santosh
    May 12, 2015 at 13:54
  • Yes. See the answer. That is what I suspected
    – Saifur
    May 12, 2015 at 13:57

2 Answers 2

6

@Saifur has explained nicely regarding the issue. So, I will just put the code that will see you through

List <WebElement> listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));
WebDriverWait wait = new WebDriverWait(wd, 20); //Wait time of 20 seconds

for (int i=1; i<=listofItems.size(); i++)
{ 
    /*Getting the list of items again so that when the page is
     navigated back to, then the list of items will be refreshed
     again */ 
    listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));

    //Waiting for the element to be visible
    //Used (i-1) because the list's item start with 0th index, like in an array
    wait.until(ExpectedConditions.visibilityOf(listofItems.get(i-1)));

    //Clicking on the first element 
    listofItems.get(i-1).click();
    wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    System.out.print(i + " element clicked\t--");
    System.out.println("pass");
    wd.navigate().back(); 
} 

So, above I have just tweaked your code a bit and have the relevant comments where changes are made and why. Hope this works out for you. :)

1
  • This worked like a charm for my solution around a button on a grid with a dynamic ID for every row. Jun 17, 2016 at 23:25
3

The possible issue with this is the DOM refresh. You cannot find a list and click through the elements back and forth and reference to the same list since the DOM has refreshed after first click. The best solution of that problem is to find the element on the fly. Plus, implicit wait is firm for that driver instance once you set that. So, you do not have to set the wait for each element look up. Instead set it where you instantiate the driver.(possibly). However, I think the explicit wait is a best fit here.

By byXpath = By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img");
List <WebElement> listofItems = wd.findElements(byXpath);
for (int i=1; i<=listofItems.size(); i++)
{
    //I would suggest you to see if you can improve the selector though
    By by= By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img[" + i + "]");
    WebElement myDynamicElement = (new wd(driver, 10))
    .until(ExpectedConditions.presenceOfElementLocated(by));
    System.out.println(i);
    myDynamicElement.click();
    wd.navigate().back();
}
3
  • Hi Saifur, Thanks a lot for instant replay, can you please tell me few more info of below code which you added. i am new in selenium and also in devlopment so may be this could be a simple question for you?. WebElement myDynamicElement = (new wd(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(by));
    – Santosh
    May 12, 2015 at 14:11
  • @Santosh That code directly uses the By selctor right before. I am using a wait mechanism which is known as Explicit wait. It waits for the element to load in the page. Read about that here
    – Saifur
    May 12, 2015 at 14:21
  • still its not looping till end of list. PLEASE ASSIST. By byxpath = By.xpath("//*[contains(@id,'dealView')]//img"); List <WebElement> listofItems = wd.findElements(byxpath); for(int i=1; i<=listofItems.size(); i++) { //System.out.println("value is" + i ); By by = By.xpath("//*[contains(@id,'dealView')]//img[" + i + "]"); WebDriverWait wait = new WebDriverWait(wd, 60); wait.until(ExpectedConditions.presenceOfElementLocated(by)).click(); wd.navigate().back(); System.out.println(i); }
    – Santosh
    May 13, 2015 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.