Help me out how to handle Browser pop up window using selenium webdriver

1,501 views
Skip to first unread message

suryanarayana mangena

unread,
Jul 29, 2011, 11:34:02 AM7/29/11
to Selenium Users
Hi Guys,

Please help me out in handling the Browser pop up window using
selenium web driver, here the case is a new browser window is opening
with in the main window browser, I want to handle that to select the
item in new browser.

I worked below code but it returns null,

String currentWindowHandle=driver.getWindowHandle();//get the main
window handle.
System.out.println(currentWindowHandle);
// Set<String> openWindowsList=driver.getWindowHandles();//It
should return set with size=2,while it returns only one.It doesnt
return the pop up window handle.

String popUpWindowHandle=null;//Variable initialized for newly
opened window with null.It remains null even after the condition in
for loop below.
for(String windowHandle:driver.getWindowHandles())
{
if (!windowHandle.equals(currentWindowHandle))
popUpWindowHandle=windowHandle;
System.out.println(popUpWindowHandle);
}
driver.switchTo().window(popUpWindowHandle);


Thanks,
Surya

Moises Siles

unread,
Jul 29, 2011, 4:24:41 PM7/29/11
to seleniu...@googlegroups.com
Take a look in this code, I saw it in a previous post, maybe that could helps

                curWindows = driver.getWindowHandles();
                driver.clickSomething();
                newWindows = driver.getWindowHandles();
                newWindows.remove(curWindows);
                driver.switchTo().window(newWindows.iterator().next());


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Vikram

unread,
Aug 11, 2011, 8:09:34 AM8/11/11
to seleniu...@googlegroups.com
Hi msiles,

thanks for the solution , I've tried it and came across below issue

code is as below

String sNewWindow = driver.getWindowHandle();
   driver.switchTo().window(sNewWindow);
   System.out.println(driver.getTitle());
   driver.close();
   
   driver.switchTo().window(sCurrent);
   System.out.println(driver.getTitle());


Problem: instead of closing new window , it's closing old one itself.

                    clicked on "What is AdSense?" link



please let me know what is wrong with the code ?

other observation driver.getTitle() is returning same result for both pages , is this causing the wrong window getting closed ?

what exactly windows handle has in it ?

thanks,
Vikram




Krishnan Mahadevan

unread,
Aug 11, 2011, 11:50:03 AM8/11/11
to seleniu...@googlegroups.com
After the point wherein you opened a new window, I think you would need to do a driver.getWindowHandles() which would list out the window handles of all the open windows, then find your window that needs to be closed, switch over to it and close it off.


Something like this  (This is crashing my firefox, which I would need to figure out why :) but this is something that can help you get started )

FirefoxDriver driver = new FirefoxDriver();
driver.findElement(By.xpath("//a[@id='helpLnk']")).click();
String currentWindow =  driver.getWindowHandle();
driver.findElement(By.xpath("//a[text()='sign-in help']")).click();
Set<String> s = driver.getWindowHandles();
Iterator<String> iterators = s.iterator();
String nameOrHandle = null;
while (iterators.hasNext()){
String id = iterators.next();
System.out.println(id);
if (! currentWindow.equalsIgnoreCase(id)){
nameOrHandle = id;
}
}
System.out.println(driver.getTitle());
driver.switchTo().window(nameOrHandle).close();
System.out.println(driver.getTitle());
driver.quit();


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/qGz7JoZGJTYJ.

Hien Ngo

unread,
Aug 11, 2011, 12:33:30 PM8/11/11
to seleniu...@googlegroups.com
Try this command:
driver.SwitchTo().Window(driver.WindowHandles.ElementAt(1).ToString()).Close();

Vikram

unread,
Aug 12, 2011, 6:21:45 AM8/12/11
to seleniu...@googlegroups.com
Thanks Krishnan and Lerry for clarifications.

So the mistake what I did was getting only single window handle; instead of getting both windows' handle once new window is opened ( after clicking a link in parent window )

Regards,
Vikram

sachin poovaiah

unread,
Feb 25, 2013, 4:50:57 AM2/25/13
to seleniu...@googlegroups.com
Hi ,
 
@Krishnan Once the driver is shifted to child window and if we close that child window ,how to retain the driver back to the parent window ??? 
 
 
Is that possible ??
 
Regards,
Sachin

Krishnan Mahadevan

unread,
Feb 25, 2013, 5:37:00 AM2/25/13
to seleniu...@googlegroups.com
Have you tried using driver.switchTo().defaultContent() to return to the parent window, then try closing the child window ?
I haven't had too many instances wherein I need to play around with windows, so I wouldn't know for sure.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/deHzC76RCxcJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

sirus tula

unread,
Feb 25, 2013, 10:05:54 AM2/25/13
to seleniu...@googlegroups.com
Sachin, use this code to switch between different windows. Yes, you're welcome :).
 
    String originalHandle = methods.getWindowHandle();
   driver.findElement(<element1>).click();                                       //clicks on element 1 on page 1
   methods.manage().timeouts().implicitlyWait(900, TimeUnit.MILLISECONDS);
for (String winHandle : methods.getWindowHandles()) { //for loop to switch between new window.
methods.switchTo().window(winHandle);
}
WebDriverWait wait = new WebDriverWait(methods, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(Element2)); //wait until element2 in page 2 is present
//Do some stuff on page 2
methods.close();
methods.switchTo().window(originalHandle); //switch back to original page



--
 
- "If you haven't suffered, you haven't lived your life."
 
Thanks,
 
Sirus

sirus tula

unread,
Feb 25, 2013, 10:06:21 AM2/25/13
to seleniu...@googlegroups.com
Typo, methods = driver;

sachin poovaiah

unread,
Feb 25, 2013, 11:51:26 AM2/25/13
to seleniu...@googlegroups.com
Hi ,

@Krishnan, 
I tried switching to child window and closing the child window. When the control is in child window , the child window will have the driver control and if the driver is closed at that instance the driver will be closed . Once the driver is closed the test fails as driver only is closed.


@sirus, 
i havent tried your code still.. Thanks anyways :) surely i will have a look at it and get back to the post :) 
thanks for the help guys


regards,
Sachin

sachin poovaiah

unread,
Feb 26, 2013, 2:13:53 AM2/26/13
to seleniu...@googlegroups.com
@ Sirus:
 
Thanks a lot :) you rcode wroked fine :) eventhough i am runnin g tests parallely in IE and FF and code works fine.. thanks a ton...  I was not switching to the original handle so used to get errors asdriver ended .
Now its working fine :)

sirus tula

unread,
Feb 26, 2013, 9:41:08 AM2/26/13
to seleniu...@googlegroups.com
Pleasure is all mine, Sachin.
 
Glad that the pain that i went through to get this code working helped you :).
 


 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/dbU-vNtN1fYJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages