eclipse - selenium find element in a while loop in java -


so, have selenium webdriver command condition while loop shown here:

            while (driver.findelement(by.cssselector("a[action='cancel']")).isdisplayed() == true){             driver.navigate().refresh();              timeunit.seconds.sleep(5);             driver.findelement(by.id("479510558845313")).sendkeys(instagramaievx.spamusernameinput);              driver.findelement(by.id("263795143794707")).sendkeys(instagramaievx.spamcommentinput);              driver.findelement(by.id("u_0_4")).sendkeys(x);             driver.findelement(by.id("u_0_5")).click();         }          timeunit.seconds.sleep(2);         driver.findelement(by.xpath("//a[contains(text(),'okay')]")).click();         killfirefox(); 

so, problem when condition false, not skip while loop , go what's below it. tries : driver.findelement(by.cssselector("a[action='cancel']")) makes program fail. how make skip when it's false ands go what's out of loop? thanks.

you need handle case when element not present before checking if it's visible:

while (true){     webelement button = null;     try {       button = driver.findelement(by.cssselector("a[action='cancel']"));     } catch (nosuchelementexception ex){       break;   // button missing, exit loop     }     if (button.isdisplayed() == false) {       break;   // button hidden, exit loop     }      driver.navigate().refresh();      timeunit.seconds.sleep(5);     driver.findelement(by.id("479510558845313")).sendkeys(instagramaievx.spamusernameinput);      driver.findelement(by.id("263795143794707")).sendkeys(instagramaievx.spamcommentinput);      driver.findelement(by.id("u_0_4")).sendkeys(x);     driver.findelement(by.id("u_0_5")).click(); } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -