EDIT 4:
EDIT 3
EDIT 2
string currentWindow = driver.CurrentWindowHandle;
driver.SwitchTo().Window("");
string childTitle = driver.Title;
driver.SwitchTo().Window(currentWindow);
string parentTitle = driver.Title;
上面的代码为我提供了父窗口或子窗口相同的标题。
EDIT:
<a id="ctl00_ctl00_Features_ctl03_lnkPage" class="title" target="_blank" href="websiteaddress">Stay Around</a>
如何验证新打开的窗口的标题,并在验证后关闭打开的新窗口?
因此,在我的页面中,我有一个链接,单击该链接会打开一个新窗口,现在我不确定如何验证该窗口的标题。
这是我到目前为止所做的。
GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
//它打开一个新窗口
现在我想将焦点切换到新窗口并验证标题并关闭新窗口
返回上一个窗口。
大多数人在处理 IE 中的弹出窗口时会忽略的一点是,元素上的单击是异步的。也就是说,如果你检查.WindowHandles
单击后立即更改属性,您可能会失去竞争条件,因为您在 IE 有机会创建新窗口之前检查是否存在新窗口,并且驱动程序有机会注册它的存在。
下面是我用来执行相同操作的 C# 代码:
string foundHandle = null;
string originalWindowHandle = driver.CurrentWindowHandle;
// Get the list of existing window handles.
IList<string> existingHandles = driver.WindowHandles;
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
// Use a timeout. Alternatively, you could use a WebDriverWait
// for this operation.
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(5));
while(DateTime.Now < timeout)
{
// This method uses LINQ, so it presupposes you are running on
// .NET 3.5 or above. Alternatively, it's possible to do this
// without LINQ, but the code is more verbose.
IList<string> currentHandles = driver.WindowHandles;
IList<string> differentHandles = currentHandles.Except(existingHandles).ToList();
if (differentHandles.Count > 0)
{
// There will ordinarily only be one handle in this list,
// so it should be safe to return the first one here.
foundHandle = differentHandles[0];
break;
}
// Sleep for a very short period of time to prevent starving the driver thread.
System.Threading.Thread.Sleep(250);
}
if (string.IsNullOrEmpty(foundHandle))
{
throw new Exception("didn't find popup window within timeout");
}
driver.SwitchToWindow(foundHandle);
// Do whatever verification on the popup window you need to, then...
driver.Close();
// And switch back to the original window handle.
driver.SwitchToWindow(originalWindowHandle);
顺便说一句,如果您使用 .NET 绑定,您可以访问PopupWindowFinder
WebDriver.Support.dll 程序集中的类,它使用非常相似的方法来定位弹出窗口。您可能会发现该类完全满足您的需求,并且无需修改即可使用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)