0

I want to select the current window,frame or popup where my browser is pointing. (i.e when i doubleclick or click it opens an popup or window) I want to get through that window or popup using selenium webdriver.So if possible please suggest me how to do it. Thanks in advance.

asked Feb 27, 2014 at 13:52

3 Answers 3

1

To switch to new pop up window what you can do is the following:

String currentWindow = driver.getWindowHandle();
String lastWindow = null;
 Set<String> handles = driver.getWindowHandles();
 for (String aux : handles) {
 lastWindow = aux;
 }
 driver.switchTo().window(lastWindow);

This will switch you to the new window opened. If you need to switch to the other window just switch to currentWindow.

Hope this helps.

answered Feb 27, 2014 at 16:02
1
  • sure i will work on this. Commented Feb 28, 2014 at 4:44
5

You can use the following code also to handle popup.

 String mainwindow = driver.getWindowHandle();
 for (String popup : driver.getWindowHandles()){
 driver.switchTo().window(popup);
 }
// Your code on poppup window
 driver.switchTo().window(mainwindow); //Switching to main/parent window

Hope this will help :)

answered Mar 4, 2014 at 9:11
1
  • sure i will try it QA4it Commented Mar 4, 2014 at 14:51
0

As an alternative you can try this,

Set <String>handles = driver.getWindowHandles();//To handle multiple windows
firstWinHandle = driver.getWindowHandle(); //To get your main window
handles.remove(firstWinHandle);
String winHandle=handles.iterator().next(); //To find popup window
if (winHandle!=firstWinHandle){
secondWinHandle=winHandle;
driver.switchTo().window(secondWinHandle); //To switch to popup window

Make sure to add some delay after switching to next popup, Thread.sleep(5000);

answered Oct 23, 2015 at 9:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.