0

I am relatively new to coding.

Below is the code for my program which opens a windows(flipkart.com)> proceeds to the secondary window by clicking an icon inside the web-page. Now I want to generate the window handles for both first and second page but it gives me errors as given below

Programs:

package MoinPrograms;

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.firefox.FirefoxDriver;

public class MouseOverActions {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.flipkart.com/");
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
        driver.findElement(By.xpath("// button [@class=\"_2AkmmA _29YdH8\"]")).click();
        driver.findElement(By.xpath("//p [@class=\"_1AuloC\"and text()= \"Mobiles & Tablets\"]")).click();
        Set<String> FlipKart = driver.getWindowHandles();
        Iterator<String> k = FlipKart.iterator();
        String handle1 = k.next();
        String handle2 = k.next();
        System.out.println(handle1);
        System.out.println(handle2);
    }

}

Errors :

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(Unknown Source)
    at java.base/java.util.LinkedHashMap$LinkedKeyIterator.next(Unknown Source)
    at MoinPrograms.MouseOverActions.main(MouseOverActions.java:24)
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Moin Ahmed
  • 49
  • 2
  • 8
  • How are you dealing with the login pop-up when you first go to the page? It could be that your code cannot find the elements because it is first being presented with a login pop-up. – Chris C May 16 '18 at 12:22

4 Answers4

0

Use a while-loop to make sure to get all elements:

Iterator<String> k = FlipKart.iterator();
while (k.hasNext()) {
        String handle = k.next();
        System.out.println(handle);
}
Würgspaß
  • 4,660
  • 2
  • 25
  • 41
0

you need to check yout list to make sure it has an element by using hasNext() method.

package MoinPrograms;

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.firefox.FirefoxDriver;

public class MouseOverActions {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.flipkart.com/");
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
        driver.findElement(By.xpath("// button [@class=\"_2AkmmA _29YdH8\"]")).click();
        driver.findElement(By.xpath("//p [@class=\"_1AuloC\"and text()= \"Mobiles & Tablets\"]")).click();
        Set<String> FlipKart = driver.getWindowHandles();
        Iterator<String> k = FlipKart.iterator();
        while(k.hasNext()){
          String handle1 = k.next();
          System.out.println(handle1);
        }
    }

}
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
0

There is only one handle available in your code :

You can get it by using this code :

ArrayList<String> flipkart = new ArrayList<String>(driver.getWindowHandles()); 
        Iterator<String> k = flipkart.iterator();
        while (k.hasNext()) {
                String handle = k.next();
                System.out.println(handle);
        }
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

java.util.nosuchelementexception

As per the Java Docs of java.util.nosuchelementexception is thrown by various accessor methods to indicate that the element being requested does not exist.

Now if you look at the Java Docs of next() method it clearly mentions next() method returns the next element in the iteration or NoSuchElementException if the iteration has no more elements.

Root Cause of the issue

In your use case, the root cause of java.util.nosuchelementexception is as follows :

  • The next() method fails to return any element as per MouseOverActions.java:24 in your code :

    String handle2 = k.next();
    

Explaination

Before and after clicking on the element identified as By.xpath("//p [@class=\"_1AuloC\"and text()= \"Mobiles & Tablets\"]") there is only one Window Handle and String handle1 is assigned with the Window Handle. As there is no second Window Handle available String handle2 = k.next(); raises java.util.nosuchelementexception.

Solution

You need to make the following tweaks to your own code :

  • Remove the line of code :

    String handle2 = k.next();
    
  • Remove the line of code :

    System.out.println(handle2);
    
  • As Firefox Browser opens in maximized mode you don't need to maximize it further, so remove the line :

    driver.manage().window().maximize();
    
  • Update the Selenium Java Client to v3.12.0 and use the following JVM property inconjunction with GeckoDriver :

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    
  • Your own code block to print the Window Handle is as follows :

    System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.flipkart.com/");
    driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
    driver.findElement(By.xpath("// button [@class=\"_2AkmmA _29YdH8\"]")).click();
    driver.findElement(By.xpath("//p [@class=\"_1AuloC\"and text()= \"Mobiles & Tablets\"]")).click();
    Set<String> FlipKart = driver.getWindowHandles();
    Iterator<String> k = FlipKart.iterator();
    String handle1 = k.next();
    System.out.println(handle1);
    
  • Console Output :

    4294967297
    

Here you can find a detailed discussion on Best way to keep track of Windows with Selenium in IE11?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352