Reputation: 11
I wrote a code in webdriver for getting the window id of tab window but code throws a exception . " Exception in thread "main" java.util.NoSuchElementException " error is occured when i execute the following code.
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class web_windows {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.icicibank.com/");
Set<String> winids = driver.getWindowHandles();
Iterator<String> iterate = winids.iterator();
System.out.println(iterate.next());
driver.findElement(By.xpath("//*[@id='footer_container']/p/a[1]")).click();
winids = driver.getWindowHandles();
iterate = winids.iterator();
String firstwindow = iterate.next(); // window id of main window
String tabwindow = iterate.next(); // window id of tabbed window
// main tab
System.out.println(firstwindow);
// tabbed window
System.out.println(tabwindow);
}
}
After execting above code following error is coming:
Exception in thread "main" java.util.NoSuchElementException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(Unknown Source)
at java.util.LinkedHashMap$KeyIterator.next(Unknown Source)
at web_windows.main(web_windows.java:
Upvotes: 0
Views: 2018
Reputation: 12880
You have to check if it exists before you access it
while(iterate.hasNext()){
String firstwindow = iterate.next();
}
Documentation states that
boolean hasNext()
Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
Returns: true if the iteration has more elements
iterate.next()
Throws: NoSuchElementException - if the iteration has no more elements
This is what happens in your case.
Upvotes: 2
Reputation: 1133
I assume that your winids is empty, most likely your xpath is wrong, without having a look at the webpage I suggest you test your xpath, maybe using jquery and a javascript console in your browser
Upvotes: 0