Java – loop for checking the list of items in selenium webdriver

I have written the code for the check list web element below, but the following code is running, but only the first item does not loop to the end of the 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();
}

Solution

@Saifur explained the problem well So I will provide you with the code

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, I tweaked your code a little above and got comments about the changes and why I hope it works for you

The above is the Java compiled by the programming house for you to check all the contents of the cycle of item list in selenium webdriver. I hope this article can help you solve the program development problems encountered by java to check the cycle of item list in selenium webdriver.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>