我在使用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);
语句,因为如果窗口需要更多的时间来打开,代码可能会失败,如果窗快开那么它使测试不必要的慢。
有什么方法可以知道窗口何时打开,然后测试可以恢复执行?
感谢您的答复。它正在工作。 – Ozone 2012-02-14 04:45:41
可能会出现新标签打开但处理尚未添加到驱动器实例的情况。我的解决方案是在点击获取当前句柄计数之前,然后在循环内检查计数是否改变。然后切换到新打开的选项卡,如'driver.switchTo()。window(handles [handles.count() - 1]);'每个迭代更新'handles'。 – Edgar 2015-12-17 11:56:51