使用C#在WebDriver中打开新窗口 [英] Opening new window in WebDriver using C#

查看:591
本文介绍了使用C#在WebDriver中打开新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑3

编辑2

    string currentWindow = driver.CurrentWindowHandle;

    driver.SwitchTo().Window("");
    string childTitle = driver.Title;

    driver.SwitchTo().Window(currentWindow);
    string parentTitle = driver.Title;

上面的代码为我的父窗口或子窗口提供了相同的标题.

the above code gives me the same title for parent window or child window.

<a id="ctl00_ctl00_Features_ctl03_lnkPage" class="title" target="_blank" href="websiteaddress">Stay  Around</a>

如何验证新打开的窗口的标题,一旦我验证然后关闭打开的新窗口?

how to verify the title of a newly window open and once i verified then close the opened new window?

所以在我的页面中,我有一个链接,然后单击链接,它会打开一个新窗口,现在我不确定如何验证该窗口的标题.

so in my page I have a link and click on the link and it opens a new window and now I am not sure how to verify the title of that window.

这是我到目前为止所做的.

here is what i have done so far.

GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();

//它会打开一个新窗口

//it opens a new window

现在我想将焦点切换到新窗口并验证标题并关闭新窗口 返回上一个窗口.

now i want to switch focus on the new window and verify the title and close the new window back to the previous window.

推荐答案

大多数人在处理IE中的弹出窗口时会错过的一环是,对元素的单击是异步的.也就是说,如果单击后立即检查.WindowHandles属性,则可能会丢失竞争条件,因为在IE有机会创建新窗口之前,您正在检查是否存在新窗口,并且驱动程序有机会注册它的存在.

The piece that most people miss when dealing with popup windows in IE is that a click on an element is asynchronous. That is to say, if you check the .WindowHandles property immediately after a click, you may lose the race condition, because you're checking for the existence of a new window before IE has had the chance to create it, and the driver has had a chance to register it exists.

这是我用来执行相同操作的C#代码:

Here's the C# code I would use to perform the same operation:

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绑定,则可以访问WebDriver.Support.dll程序集中的PopupWindowFinder类,该类使用与查找弹出窗口非常相似的方法.您可能会发现该类完全符合您的需求,并且可以直接使用它而无需修改.

Incidentally, if you're using the .NET bindings, you have access to a PopupWindowFinder class in the WebDriver.Support.dll assembly, which uses a very similar approach to the locating popup windows. You may find that class meets your needs exactly, and can use it without modification.

这篇关于使用C#在WebDriver中打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆