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.
3 Answers
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.
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 :)
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);