NoSuchElementException on running code with window handlers | Selenium Forum
M
Posted on 04/04/2016
Following is my code
================
[b:2r6u602o]Query :My code seems ok.But i get NoSuchElement Exception.[/b:2r6u602o]

package p1;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TimesOFWindow {
public static WebDriver w=new FirefoxDriver();
public static void main(String[] args) {
// TODO Auto-generated method stub


w.navigate().to("http://timesofindia.indiatimes.com");
w.manage().timeouts().implicitlyWait(30, TimeUnit.MILLISECONDS);
w.findElement(By.xpath("//a[text()='India']")).click();
w.navigate().back();
w.manage().timeouts().implicitlyWait(30, TimeUnit.MILLISECONDS);
WebElement Box= w.findElement(By.xpath("//*[@id='footer']/div[2]/div/div[1]/div[1]"));


Box.findElement(By.xpath("//a[text()='Indiatimes']")).click();
Set <String> s =new HashSet<String>();
Iterator <String>it=s.iterator();
s=w.getWindowHandles();
System.out.println(s.size());

Object[] WinIDs=new Object[s.size()];
//WinIDs[0]=w.getWindowHandle();
String mainWindow=it.next();
String childwin=it.next();
System.out.println(mainWindow);
System.out.println(childwin);

}}

M
Replied on 04/04/2016

Following error i get :

Exception in thread "main" java.util.NoSuchElementException

at java.util.HashMap$HashIterator.nextNode(Unknown Source)
at java.util.HashMap$KeyIterator.next(Unknown Source)
at p1.TimesOFWindow.main(TimesOFWindow.java:35)


M
Replied on 04/04/2016

Even if order is not der in hashset : i thought things would work as in attachment.

[b:2kq71p15]But its not working.Need help sir.[/b:2kq71p15]

Responsive image

M
Replied on 04/04/2016

try switch to manually without iterator.


M
Replied on 04/04/2016

Sir , am suppose to switch to childwindow.... but without windowId .How do i switch to that window ?
So am using String [b:3kopgpvt]childwin=It.next()[/b:3kopgpvt] to extract the windowID.

How do i make It.next() work?
please suggest how do i switch to childwindow other than driver.switchTo.window(<windowID>)


M
Replied on 04/04/2016

Am getting exceptions bcoz... It.next() is returning null sir.

Now how do i switch to a window, whose winID is null ?:O


M
Replied on 09/04/2016

is your problem solved chaitra?


M
Replied on 10/04/2016

no sir.

If i get windId=null, how do i switch to that respective window?


M
Replied on 11/04/2016

when you iterate you have to use try catch statement inside iterator loop so you don't get any exception.