webdriver - 使用 C# 在 WebDriver 中打开新窗口

标签 webdriver selenium-webdriver

编辑4:

enter image description here 编辑3 enter image description here

编辑2

    string currentWindow = driver.CurrentWindowHandle;

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

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

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

编辑:

<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 绑定(bind),则可以访问 WebDriver.Support.dll 程序集中的 PopupWindowFinder 类,该类使用与定位弹出窗口非常相似的方法。您可能会发现该类完全满足您的需求,并且无需修改即可使用。

关于webdriver - 使用 C# 在 WebDriver 中打开新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13244448/

相关文章:

selenium - 如何使用 Geb/WebDriver 处理服务器身份验证

java - Selenium WebDriver 和下拉框

C#.NET : Scraping dynamic (JS) websites

java - IndexOutOfBoundsException:索引:1,大小:1

javascript - Selenium 与 JavaScript 和 Nodejs : How to count elements on page?

python - 如何忽略缩放设置

java - 如何在 WebDriver 中切换实例

python - 'selenium.common.exceptions.WebDriverException : Message: u'chrome not reachable

java - 在 selenium webdriver 中无需定位器即可验证文本的存在和可见性

java - 使用 webdriver API 实现 BDD (JBehave+Webdriver)