Hey there! any question in your mind? Ask It Now!.

Popular Categories





How to switch another window in Selenium Web Driver if we dont know the name of new window?

+7 votes
1,665 views
asked in Software Testing by tester007 (920 points)

In my test case a new window is opening and i have to perform action on that window then have to come back to orignal window. 

driver.switchTo().window("winname")

Using this but dont know the sindow name which is opening after click on a button.

Plese help me how can i manage new window.

1 Answer

+3 votes
answered by seleniumGuru Expert (4,570 points)
selected by tester007
 
Best answer

Its a good question if we dont know the name of window so how can we switch to another window, For this we can use this 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);
in this code threw window handler we are all avalable window then we are performing some action on that new window and coming back to orignal window ,i hope you got the point.
commented by maan (120 points)
@pankaj see this for(String winHandle : driver.getWindowHandles()){
  driver.switchTo().window(winHandle);
}
this loop will count your all open window of web driver and then perform action any window in which you want. try this once.
commented by tester007 (920 points)
yes @Maan is right its working. i have used it.
commented by anonymous
you can use its index instead of the name, another way to grab window name is using windows functiobs like getwindowname....

Related Questions

+1 vote
1 answer 538 views

Not a Member yet?

Ask to Folks Login

My Account
260 Folks are online
7 members and 253 guest online
Your feedback is highly appreciated