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 3
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.
-
sure i will work on this.Emmanuel Angelo.R– Emmanuel Angelo.R2014年02月28日 04:44:55 +00:00Commented Feb 28, 2014 at 4:44
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 :)
-
sure i will try it QA4itEmmanuel Angelo.R– Emmanuel Angelo.R2014年03月04日 14:51:07 +00:00Commented Mar 4, 2014 at 14:51
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);
Explore related questions
See similar questions with these tags.