简体   繁体   中英

How to switch to new window without using selenium/webdriver methods

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.

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.com/p/selenium/wiki/InternetExplorerDriver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM