I have three variations, based on what you are going to click on. I do use a waitForElementPresent() internal method, which should be obvious what it does based on the name. Also, I am using WebDriverBackedSelenium, which is why you see a selenium.waitForPageToLoad() in there. I also have a timer that is available and used to do timeouts to insure we don't end up waiting forever (again, fairly obvious how it works):
Mike
Here is the code:
/**
* This is used when clicking on some page element causes a new window to
* pop up, which can either be a new floating window or a new tab. It will
* do the click on the element and then switch to the new window. It
* returns the handle for the current window so you can switch back to it
* once you have finished with the pop up window. It will ignore "Timed out"
* errors in the belief that they are false<br/>
* <br/>
* After we switch to the new window we do a
* <code>selenium.waitForPageToLoad()</code> before returning to the caller.
* Be sure to check for the first element you wish to interact with right,
* or what you think is the last of the dynamic page elements created by
* JavaScript, after this call returns to be sure it is present using
* <code>helper.waitForElementPresent()</code>.
* @param locator The locator string to do the selenium.click() for.
* @throws Exception
* @see #waitForElementPresent(java.lang.String)
* @see #waitForElementPresent(java.lang.String, int)
* @see #waitForElementPresent(
org.openqa.selenium.By)
* @see #waitForElementPresent(
org.openqa.selenium.By, int)
*/
public String clickAndPopupWindow(String locator) throws Exception
{
String newWindow;
Iterator<String> iterator;
String oldWindow = driver.getWindowHandle();
Set<String> oldSet = driver.getWindowHandles();
Set<String> newSet = oldSet;
try
{ // PENDING If this solves the false timeout we need a click() wrapper method
waitForElementPresent(locator);
selenium.click(locator);
} // try
catch (Exception ex)
{ // Check to make sure the exception is valid before throwing it
if (!ex.getMessage().startsWith(timedOutPrefix))
throw ex;
} // catch
timer.setInitialDelay(timeout);
timer.start();
while (newSet.size() == oldSet.size() || timerExpired)
{ // Wait for the new window to be displayed
Thread.sleep(100); // Wait .1 seconds
newSet = driver.getWindowHandles(); // See if the new window is up
} // while
timer.stop();
if (timerExpired)
throw new Exception("No new window was created after clicking: " +
locator);
iterator = newSet.iterator();
do
{ // Find which one is the new window
newWindow = iterator.next();
} while (oldSet.contains(newWindow) && iterator.hasNext());
driver.switchTo().window(newWindow);
selenium.waitForPageToLoad(timeoutString);
return oldWindow;
} // clickAndPopupWindow(String locator)
/**
* This is used when clicking on some page element causes a new window to
* pop up, which can either be a new floating window or a new tab. It will
* do the click on the element and then switch to the new window. It
* returns the handle for the current window so you can switch back to it
* once you have finished with the pop up window. It will ignore "Timed out"
* errors in the belief that they are false<br/>
* <br/>
* After we switch to the new window we do a
* <code>selenium.waitForPageToLoad()</code> before returning to the caller.
* Be sure to check for the first element you wish to interact with right,
* or what you think is the last of the dynamic page elements created by
* JavaScript, after this call returns to be sure it is present using
* <code>helper.waitForElementPresent()</code>.
* @param locator The By class locator to do the
* <code>driver.findElement().click()</code> for.
* @throws Exception
* @see #waitForElementPresent(java.lang.String)
* @see #waitForElementPresent(java.lang.String, int)
* @see #waitForElementPresent(
org.openqa.selenium.By)
* @see #waitForElementPresent(
org.openqa.selenium.By, int)
*/
public String clickAndPopupWindow(By locator) throws Exception
{
String newWindow;
Iterator<String> iterator;
String oldWindow = driver.getWindowHandle();
Set<String> oldSet = driver.getWindowHandles();
Set<String> newSet = oldSet;
try
{ // PENDING If this solves the false timeout we need a click() wrapper method
waitForElementPresent(locator);
driver.findElement(locator).click();
} // try
catch (Exception ex)
{ // Check to make sure the exception is valid before throwing it
if (!ex.getMessage().startsWith(timedOutPrefix))
throw ex;
} // catch
timer.setInitialDelay(timeout);
timer.start();
while (newSet.size() == oldSet.size() || timerExpired)
{ // Wait for the new window to be displayed
Thread.sleep(100); // Wait .1 seconds
newSet = driver.getWindowHandles(); // See if the new window is up
} // while
timer.stop();
if (timerExpired)
throw new Exception("No new window was created after clicking: " +
locator.toString());
iterator = newSet.iterator();
do
{ // Find which one is the new window
newWindow = iterator.next();
} while (oldSet.contains(newWindow) && iterator.hasNext());
driver.switchTo().window(newWindow);
selenium.waitForPageToLoad(timeoutString);
return oldWindow;
} // clickAndPopupWindow(By locator)
/**
* This is used when clicking on some page element causes a new window to
* pop up, which can either be a new floating window or a new tab. It will
* do the click on the element and then switch to the new window. It
* returns the handle for the current window so you can switch back to it
* once you have finished with the pop up window. It will ignore "Timed out"
* errors in the belief that they are false<br/>
* <br/>
* After we switch to the new window we do a
* <code>selenium.waitForPageToLoad()</code> before returning to the caller.
* Be sure to check for the first element you wish to interact with right,
* or what you think is the last of the dynamic page elements created by
* JavaScript, after this call returns to be sure it is present using
* <code>helper.waitForElementPresent()</code>.
* @param element The <code>WebElement</code> to do the <code>.click()</code> for.
* @throws Exception
* @see #waitForElementPresent(java.lang.String)
* @see #waitForElementPresent(java.lang.String, int)
* @see #waitForElementPresent(
org.openqa.selenium.By)
* @see #waitForElementPresent(
org.openqa.selenium.By, int)
*/
public String clickAndPopupWindow(WebElement element) throws Exception
{
String newWindow;
Iterator<String> iterator;
String oldWindow = driver.getWindowHandle();
Set<String> oldSet = driver.getWindowHandles();
Set<String> newSet = oldSet;
try
{ // PENDING If this solves the false timeout we need a click() wrapper method
element.click();
} // try
catch (Exception ex)
{ // Check to make sure the exception is valid before throwing it
if (!ex.getMessage().startsWith(timedOutPrefix))
throw ex;
} // catch
timer.setInitialDelay(timeout);
timer.start();
while (newSet.size() == oldSet.size() || timerExpired)
{ // Wait for the new window to be displayed
Thread.sleep(100); // Wait .1 seconds
newSet = driver.getWindowHandles(); // See if the new window is up
} // while
timer.stop();
if (timerExpired)
throw new Exception("No new window was created after clicking: " +
element.toString());
iterator = newSet.iterator();
do
{ // Find which one is the new window
newWindow = iterator.next();
} while (oldSet.contains(newWindow) && iterator.hasNext());
driver.switchTo().window(newWindow);
selenium.waitForPageToLoad(timeoutString);
return oldWindow;
} // clickAndPopupWindow(WebElement element)
On Friday, June 22, 2012 3:19:12 AM UTC-7, Mallikarjun Yalagi wrote:
mike,
can you share the code, i tried still not working.
Thanks&Regards
Mallikarjun Yalagi
Hi siddu,
Thanks for the reply.you mean we can get windowids using driver.getwindow handles() function and store it in one varible and call that varible in switchTo function right.
Thanks&Regards
Mallikarjun Yalagi
Hi
we can handle popup windows using swithTo() ---function
driver.swithTo().window(windowId);
we can get windowids using----- driver.getwidnowhandles();
On Thursday, June 21, 2012 4:02:08 PM UTC+5:30, Mallikarjun Yalagi wrote:
Hi,
when i click a popup window will come and need to perform some action on that window.how to handle that popup window using selenium web driver?
Thanks&Regards
Mallikarjun Yalagi
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.