Java Internet Explorer 8-10中的Selenium WebDriver窗口切换问题
我在尝试使用SeleniumWebDriver测试我们的应用程序时发现了一个问题。问题在于IE9中的不稳定弹出窗口。它并不总是可复制的,它发生在大约20%的窗口切换中,但这使得在IE上进行测试几乎是不可能的。在FireFox中,一切都很完美Java Internet Explorer 8-10中的Selenium WebDriver窗口切换问题,java,internet-explorer,selenium,webdriver,Java,Internet Explorer,Selenium,Webdriver,我在尝试使用SeleniumWebDriver测试我们的应用程序时发现了一个问题。问题在于IE9中的不稳定弹出窗口。它并不总是可复制的,它发生在大约20%的窗口切换中,但这使得在IE上进行测试几乎是不可能的。在FireFox中,一切都很完美 我尝试增加超时时间: TimeSpan interval=新的TimeSpan(0,0,10) driver.Manage().timeout().ImplicitlyWait(interval) 创建自己的对象查找方法: for (i
TimeSpan interval=新的TimeSpan(0,0,10)代码>
driver.Manage().timeout().ImplicitlyWait(interval)代码>
创建自己的对象查找方法:
for (int x = 0; x <= waitTimeOut; x++)
{
try
{
element = (IWebElement)driver.FindElement(By.XPath(obj.Xpath));
return element;
}
catch{}
}
for(int x=0;x对于IE驱动程序,不能保证窗口在集合中的显示顺序。也就是说,集合中的第0个窗口不一定是会话打开的第一个窗口。在这种情况下,您需要执行以下操作:
private string FindNewWindowHandle(IWebDriver driver, IList<string> existingHandles, int timeout)
{
string foundHandle = string.Empty;
DateTime endTime = DateTime.Now.Add(TimeSpan.FromSeconds(timeout));
while (string.IsNullOrEmpty(foundHandle) && DateTime.Now < endTime)
{
IList<string> currentHandles = driver.WindowHandles;
if (currentHandles.Count != existingHandles.Count)
{
foreach (string currentHandle in currentHandles)
{
if (!existingHandles.Contains(currentHandle))
{
foundHandle = currentHandle;
break;
}
}
}
if (string.IsNullOrEmpty(foundHandle))
{
System.Threading.Thread.Sleep(250);
}
}
// Note: could optionally check for handle found here and throw
// an exception if no window was found.
return foundHandle;
}
IList<string> handles = driver.WindowHandles;
// do whatever you have to do to invoke the popup
element.Click();
string popupHandle = FindNewWindowHandle(driver, handles, 10);
if (!string.IsNullOrEmpty(popupHandle))
{
driver.SwitchTo().Window(popupHandle);
}
私有字符串findnewindowHandle(IWebDriver驱动程序,IList existingHandles,int超时)
{
string foundHandle=string.Empty;
DateTime endTime=DateTime.Now.Add(TimeSpan.FromSeconds(timeout));
while(string.IsNullOrEmpty(foundHandle)&&DateTime.Now
上述函数的用法如下所示:
private string FindNewWindowHandle(IWebDriver driver, IList<string> existingHandles, int timeout)
{
string foundHandle = string.Empty;
DateTime endTime = DateTime.Now.Add(TimeSpan.FromSeconds(timeout));
while (string.IsNullOrEmpty(foundHandle) && DateTime.Now < endTime)
{
IList<string> currentHandles = driver.WindowHandles;
if (currentHandles.Count != existingHandles.Count)
{
foreach (string currentHandle in currentHandles)
{
if (!existingHandles.Contains(currentHandle))
{
foundHandle = currentHandle;
break;
}
}
}
if (string.IsNullOrEmpty(foundHandle))
{
System.Threading.Thread.Sleep(250);
}
}
// Note: could optionally check for handle found here and throw
// an exception if no window was found.
return foundHandle;
}
IList<string> handles = driver.WindowHandles;
// do whatever you have to do to invoke the popup
element.Click();
string popupHandle = FindNewWindowHandle(driver, handles, 10);
if (!string.IsNullOrEmpty(popupHandle))
{
driver.SwitchTo().Window(popupHandle);
}
IList handles=driver.WindowHandles;
//执行调用弹出窗口所需的任何操作
元素。单击();
字符串popupHandle=findnewindowHandle(驱动程序,句柄,10);
如果(!string.IsNullOrEmpty(popupHandle))
{
driver.SwitchTo()窗口(popupHandle);
}
字符串currentWindowHandle=driver.getWindowHandle();
driver.findElement(By.cssSelector(locator)).click();
设置windows=driver.getWindowHandles();
用于(字符串窗口:windows){
如果(!window.equals(currentWindowHandle)){
驱动程序.切换到().窗口(窗口);
driver.close();
}
}
driver.switchTo().window(currentWindowHandle);
如果是IE11,修改HKLM\U CURRENT\U USER\Software\Microsoft\Internet Explorer\Main路径应包含值为0的TabProcGrowth键。您使用的是什么版本的Selenium?我以前只有在IE中遇到类似问题,我必须在切换到命令后添加1000-5000ms睡眠来解决。谢谢,我将其转换为Java a这对我来说很有效。你太棒了。这解释了一些事情。我一直在根据索引切换IE,假设窗口句柄是按照它们打开的顺序。@djangofan:你能把转换后的java代码放在这里吗?那太好了。我也在尝试同样的事情,但没有成功。你应该解释你的答案。