admin管理员组

文章数量:1426959

Presently, the application in which I am working has got many work flows that open new window one after another. I have used the following way to switch the focus to new window:

for (String popUpHandle : driver.getWindowHandles()) {
driver.switchTo().window(popUpHandle);
if(driver.getCurrentUrl().equalsIgnoreCase(URL of the new window)
...
}

Same way, I used it with page title. Used selenium.isElementPresent present to get inside the condition and do certain operation in the new window.

The above three solutions work fine, but takes too much time in IE when in one work flow, 2-3 windows remain hidden.

Any guidance to switch focus new window as soon as we open it after we click on some link or button, would be very much appreciated.

Presently, the application in which I am working has got many work flows that open new window one after another. I have used the following way to switch the focus to new window:

for (String popUpHandle : driver.getWindowHandles()) {
driver.switchTo().window(popUpHandle);
if(driver.getCurrentUrl().equalsIgnoreCase(URL of the new window)
...
}

Same way, I used it with page title. Used selenium.isElementPresent present to get inside the condition and do certain operation in the new window.

The above three solutions work fine, but takes too much time in IE when in one work flow, 2-3 windows remain hidden.

Any guidance to switch focus new window as soon as we open it after we click on some link or button, would be very much appreciated.

Share Improve this question asked Aug 14, 2012 at 3:54 arinarin 1253 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

One trick to speed it up a little bit is to always ignore the parent window. So do something like:

//before any pop ups are open
String parentHandle = driver.getWindowHandle();

//after you have pop ups
for (String popUpHandle : driver.getWindowHandles()) {
  if(!popUpHandle.equals(parentHandle)){
    driver.switchTo().window(popUpHandle);
    if(driver.getCurrentUrl().equalsIgnoreCase(URL of the new window)){
      //do something here
    }
  }
}

You can also switch to the most recent opened window this way since the collection should be ordered (* I think, don't have access to a Selenium set up to test at the moment *):

String newWindowHandle = driver.getWindowHandles()[driver.getWindowHandles().length - 1];
driver.switchTo().window(newWindowHandle);

Finally, not sure which version of Selenium you're using but the latest IEDriver is significantly faster: http://code.google./p/selenium/wiki/InternetExplorerDriver

本文标签: javaHow to switch to new window without using seleniumwebdriver methodsStack Overflow