java - Issue Switching between Popups & Main Window -


on main window, trying click on link opens popup window. text window , close it. again repeat 40 links on main window.

but, after loop of 3-4 times, failing. not sure why happening. happening @ 'if' condition doesn't meet, may happen otherwise also.

i following error:

java.util.nosuchelementexception @ java.util.linkedhashmap$linkedhashiterator.nextentry(unknown source) @ java.util.linkedhashmap$keyiterator.next(unknown source) 

here code snippet:

for(int x=1;x<119;x+=3) {     //waitforelement.until(expectedconditions.presenceofelementlocated(by.xpath("//*[@id='resultstable']/tbody/tr["+x+"]/td[2]/a")));      webelement element = driver.findelement(by.xpath("//*[@id='resultstable']/tbody/tr["+x+"]/td[2]/a"));      actions builder = new actions(driver);     builder.movetoelement(element).click().build().perform();     driver.manage().timeouts().implicitlywait(30, timeunit.seconds);      set <string> windowids = driver.getwindowhandles();     iterator <string> iterat = windowids.iterator();      string mainwindowid = iterat.next();     string next1stwindowid = iterat.next();      driver.switchto().window(next1stwindowid);     string pagesource = driver.getpagesource();      thread.sleep(750);     if(!pagesource.contains("resume no longer available."))                       {         string name = driver.findelement(by.xpath("//*/body/table/tbody/tr[4]/td[2]/table/tbody/tr[1]/td[1]/div[2]/span[1]")).gettext();         string phone = driver.findelement(by.xpath("//*/body/table/tbody/tr[4]/td[2]/table/tbody/tr[2]/td")).gettext();         string email = driver.findelement(by.xpath("//*/body/table/tbody/tr[4]/td[2]/table/tbody/tr[3]/td")).gettext();         system.out.println("on page "+i+", detail list number "+x + "\t candidate is: " +name +"\t"+ phone+"\t"+email);                              }      thread.sleep(750);     driver.close();     thread.sleep(1000);     driver.switchto().window(mainwindowid);                                              } 

as @aimbire suggested in comments should avoid using thread.sleep as possible. instead recommend using webdriverwait class. can set maximum timeout , condition (eg. page contain text). using sleep means if server not achieve actions in same amount of time each iteration of test, inconsistencies such facing can occur.


Comments