简体   繁体   中英

For Loop within While Infinite Loop Java Selenium

Can someone please have a look at the below code. It goes in an infinite loop. Please help!

while (findelement("//*[starts-with(@class,'paging-next')]")) {
        for (int i = 0; i < Hotel_Name.size(); i++) {
            String h = Hotel_Name.get(i).getAttribute("title");
            String s = Star_Rating.get(i).getAttribute("class");
            System.out.println(h + "|" + s);
        }
    }

i'm assuming findelement this (although it most likely isn't since if it was your code wouldn't compile in this state). You need to do something with your while loop, at the moment it simply finds an element, but has no boolean value to work with. So you are returned a webelement and loop forever. Try this instead:

while (findelement("//*[starts-with(@class,'paging-next')]").isDisplayed()) {
    for (int i = 0; i < Hotel_Name.size(); i++) {
        String h = Hotel_Name.get(i).getAttribute("title");
        String s = Star_Rating.get(i).getAttribute("class");
        System.out.println(h + "|" + s);
    }
}

This way you will only loop while that element is displayed, you could also specify to loop while it is enabled or selected. The idea is you only want this to run when these conditions are true, and not when they are false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM