2012-02-08 74 views
21

我在使用selenium web驱动程序自动化web应用程序时遇到了一个问题。selenium web driver如何知道新窗口何时打开然后恢复执行

该网页上有一个按钮,点击后会打开一个新窗口。当我使用下面的代码,它抛出OpenQA.Selenium.NoSuchWindowException: No window found

WebDriver.FindElement(By.Id("id of the button that opens new window")).Click(); 
//Switch to new window 
_WebDriver.SwitchTo().Window("new window name"); 
//Click on button present on the newly opened window 
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click(); 

为了解决上述问题,我添加按钮,点击SwitchTo语句之间Thread.Sleep(50000);

WebDriver.FindElement(By.Id("id of the button that opens new window")).Click(); 
Thread.Sleep(50000); //wait 
//Switch to new window 
_WebDriver.SwitchTo().Window("new window name"); 
//Click on button present on the newly opened window 
_WebDriver.FindElement(By.Id("id of button present on newly opened window")).Click(); 

它解决了这个问题,但我不希望使用Thread.Sleep(50000);语句,因为如果窗口需要更多的时间来打开,代码可能会失败,如果窗快开那么它使测试不必要的慢。

有什么方法可以知道窗口何时打开,然后测试可以恢复执行?

回答

25

你需要切换它做任何操作之前,弹出窗口控制。通过使用这个你可以解决你的问题。

在打开弹出窗口之前,获取主窗口的句柄并保存。

String mwh=driver.getWindowHandle();

现在尝试通过执行一些动作来打开弹出窗口:

driver.findElement(By.xpath("")).click(); 

Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows 

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); 
    } 
} 
+0

感谢您的答复。它正在工作。 – Ozone 2012-02-14 04:45:41

+1

可能会出现新标签打开但处理尚未添加到驱动器实例的情况。我的解决方案是在点击获取当前句柄计数之前,然后在循环内检查计数是否改变。然后切换到新打开的选项卡,如'driver.switchTo()。window(handles [handles.count() - 1]);'每个迭代更新'handles'。 – Edgar 2015-12-17 11:56:51

9

你可以等到操作成功例如,在Python:

from selenium.common.exceptions import NoSuchWindowException 
from selenium.webdriver.support.ui import WebDriverWait 

def found_window(name): 
    def predicate(driver): 
     try: driver.switch_to_window(name) 
     except NoSuchWindowException: 
      return False 
     else: 
      return True # found window 
    return predicate 

driver.find_element_by_id("id of the button that opens new window").click()   
WebDriverWait(driver, timeout=50).until(found_window("new window name")) 
WebDriverWait(driver, timeout=10).until(# wait until the button is available 
    lambda x: x.find_element_by_id("id of button present on newly opened window"))\ 
    .click() 
+0

感谢您的答复。 – Ozone 2012-02-14 04:47:16

1

我终于找到我用下面的方法来切换到新窗口答案, ,

public String switchwindow(String object, String data){ 
     try { 

     String winHandleBefore = driver.getWindowHandle(); 

     for(String winHandle : driver.getWindowHandles()){ 
      driver.switchTo().window(winHandle); 
     } 
     }catch(Exception e){ 
     return Constants.KEYWORD_FAIL+ "Unable to Switch Window" + e.getMessage(); 
     } 
     return Constants.KEYWORD_PASS; 
     } 

要移到父窗口,我使用了以下代码:

public String switchwindowback(String object, String data){ 
      try { 
       String winHandleBefore = driver.getWindowHandle(); 
       driver.close(); 
       //Switch back to original browser (first window) 
       driver.switchTo().window(winHandleBefore); 
       //continue with original browser (first window) 
      }catch(Exception e){ 
      return Constants.KEYWORD_FAIL+ "Unable to Switch to main window" + e.getMessage(); 
      } 
      return Constants.KEYWORD_PASS; 
      } 

我认为这将帮助你在窗口之间切换。

1

我用这个来等待窗口打开,它适用于我。

C#代码:

public static void WaitUntilNewWindowIsOpened(this RemoteWebDriver driver, int expectedNumberOfWindows, int maxRetryCount = 100) 
    { 
     int returnValue; 
     bool boolReturnValue; 
     for (var i = 0; i < maxRetryCount; Thread.Sleep(100), i++) 
     { 
      returnValue = driver.WindowHandles.Count; 
      boolReturnValue = (returnValue == expectedNumberOfWindows ? true : false); 
      if (boolReturnValue) 
      { 
       return; 
      } 
     } 
     //try one last time to check for window 
     returnValue = driver.WindowHandles.Count; 
     boolReturnValue = (returnValue == expectedNumberOfWindows ? true : false); 
     if (!boolReturnValue) 
     { 
      throw new ApplicationException("New window did not open."); 
     } 
    } 

然后我把这个方法中的代码

Extensions.WaitUntilNewWindowIsOpened(driver, 2);