-1

I am unable to navigate into a new window, as it is showing the same window for both parent and child. I use this code. What is the problem?

String parent_Window = driver.getWindowHandle();
Set<String> handles =  driver.getWindowHandles();
 for(String window_Handle  : handles){
    if(!window_Handle.equals(parent_Window)){
        driver.switchTo().window(window_Handle);
        //<!--Perform operation here for new window--> 

     driver.switchTo().window(parent_Window);
        }
    }
peterh
  • 11,875
  • 18
  • 85
  • 108
Fazz
  • 101
  • 10

2 Answers2

1

Probably, you tried to switch before opening a new window. If this is the case, get the main window handle first then try opening a new window and switch to your new window(or tab).

I hope it will help.

optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
0

use the below code:

// Store the current window handle
String winHandleBefore = driver.getWindowHandle();

 // Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
 }

// Perform the actions on new window

// Close the new window, if that window no more required
driver.close();

 // Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
noor
  • 2,954
  • 2
  • 16
  • 29