当前位置: 首页 > 知识库问答 >
问题:

用C#在WebDriver中打开新窗口

百里芷阳
2023-03-14

编辑4:

编辑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();

共有1个答案

胡景澄
2023-03-14

大多数人在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绑定,则可以访问WebDriver.support.dll程序集中的PopupWindowfinder类,该类使用与定位弹出窗口非常相似的方法。您可能会发现该类完全符合您的需求,并且无需修改即可使用它。

 类似资料:
  • 我试图在Chrome中找到某种功能设置或配置文件来自动打开一个新窗口中的链接,这些链接应该在一个标签中打开,但没有找到任何令人信服的解决方案,大多数建议都是Ctrl+点击链接。

  • 我尝试了各种方法来打开在.js文件中导入的.ftl文件中的新窗口中的链接,但都不成功。 下面是.js中的代码

  • 请不要在没有完整阅读的情况下就把这当作是一个重复的问题而不屑一顾。 场景: 我必须点击我的应用程序主页上的一个链接,它打开了一个新窗口。打开新窗口后,我需要对该页面上的web元素进行一些操作。这显然是你们每个人都要处理的常见情况。 问题:点击new window后,会打开一个新窗口。当执行driver.getWinDowHandles()时,它最初会给我2个(父窗口和子窗口)窗口。但是在一两秒钟内

  • 我正在使用(Webdriver)驱动程序。关闭(关闭浏览器,而不是使用driver.quit)。关闭浏览器窗口后,我无法使用驱动程序。get(url)-引发无法访问的BrowserException。 作为一项工作,我试图得到windowHandles.size(),当它是零我想打开一个新的空白窗口,然后使用driver.get(url)。 但我找不到在Webdriver类中打开空白窗口的方法。

  • 问题内容: 我正在创建一个“共享按钮”以共享当前页面。我想获取当前页面的URL并在新窗口中打开它。我有当前的URL部分,但似乎无法使下一部分工作。 我在语法上苦苦挣扎。我想将新窗口的大小指定为。 就像是: 有任何想法吗? 问题答案: 用途: 这将创建一个标题为链接的链接,该链接将在一个新窗口中打开一个高度为570,宽度为520的新网址。

  • 我有几十个Selenium Webdriver测试。我想一次完成所有的任务。如何运行测试,使每个测试都不会打开新的Webdriver浏览器窗口?