Back To Normal

Subscribe To Our E-Mail Newsletter

Wednesday, May 23, 2012

How to Identify popup window in Selenium Webdriver ?


Couch ModePrint It





1. First Check whether its popup window or IFrame window by using IE Developer or Find bug tools.

2. If its popup window then use following code :
driver.findElement(By.xpath("")).click();

Set s=driver.getWindowHandles();

This method will help to handle of opened windows other than parent



Iterator ite=s.iterator();

while(ite.hasNext())

{

String popupHandle=ite.next().toString();

if(!popupHandle.contains(mwh))

{

driver.switchTo().window(popupHandle);

/**/here you can perform operation in pop-up window**

//After finished your operation in pop-up just select the main window again

driver.switchTo().window(mwh);

}

}



This method will help to filter out of all opened windows



1) String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.

WebDriver popup = null;

Iterator windowIterator = browser.getWindowHandles();

while(windowIterator.hasNext()) {

String windowHandle = windowIterator.next();

popup = browser.switchTo().window(windowHandle);

if (popup.getTitle().equals("Google") {

break;

}

}



2) Uncomment code for the same popup,



//action in popup "Google".

popup.findElement(By.name("q")).sendKeys("Thoughtworks");

//action in popup "Google".

popup.findElement(By.name("btnG")).submit();"



3) After the popup actions, switch the driver back to the parent window,



browser.close(); // close the popup.

browser.switchTo().window(parentWindowHandle); // Switch back to parent window.





If its IFrame then use following line.



String parentWindowHandle = driver.getWindowHandle();

driver.switchTo().Frame(driver.findelement(by.id("iframeid")));
// perform ur actions.
//revert back to parent control

driver.switchTo().window(parentWindowHandle);


Click Here For Smileys :D
:D
:)
:[
;)
:D
:O
(6)
(A)
:'(
:|
:o)
8)
(K)
(M)

6 comments:

vamshi.ponnam said...

whats the if(!popupHandle.contains(mwh))

mwh is refereed to ?

Unknown said...

i cant able to work on the webelements in the new pop up window.

Unknown said...

i cant able to work on the webelement in the new pop up window.

SMERSH009X said...

This was a great post thank you.

Here is the equivalent in python:

#to select iframe
driver.switch_to_frame(id/locator of iframe)

#to select popup
def get_popup_windows(self, window_handles, parent_window):
print "Window handles",window_handles, "parent window", parent_window
popup_windows = []
for window in window_handles:
if window != parent_window:
#print ("Adding to popup window list: "+ window)
popup_windows.append(window)

#print popup_windows

return popup_windows




current_window= driver.current_window_handle
get_popup_windows(driver.window_handles, current_window)
driver.switch_to_window(current_popup[0])

Anonymous said...

thanks ... my pop-up was actually an i-frame ... thanks for the help ... I was going in circles with "alert" syntaxes ... Thanks again.

Unknown said...

Great blog.it was so informative and useful. Thank you.keep update Selenium Testing Course
software testing course in velachery chennai

560 Free Online Courses

Top 200 universities launched 500 free online courses.  Please find the list here .