硒网络驱动程序中的弹出窗口 [英] Popup's in selenium webdrivers

查看:28
本文介绍了硒网络驱动程序中的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 c# winform 中使用 selenium firefox webdrivers,我有下面的代码来获取当您单击webtraffic_popup_start_button"时显示的弹出窗口的句柄,它应该获取弹出窗口的句柄但弹出窗口句柄和当前的一样.

So I'm working with selenium firefox webdrivers in c# winform and I have this code below to get the handle of the popup that shows when you click on the "webtraffic_popup_start_button" and it should get the handle of the popup but the popup handle is same as current one.

string current = driver.CurrentWindowHandle;
driver.FindElement(By.XPath("//*[@id='webtraffic_popup_start_button']")).Click();
Thread.Sleep(Sleep_Seconds);
popup = driver.CurrentWindowHandle;
Thread.Sleep(3000);
driver.SwitchTo().Window(current);
Thread.Sleep(1000);

如有任何帮助,将不胜感激,谢谢

Any help with this would be much appreciated thank you

这就是弹出窗口的样子.

This is what pop up looks like.

推荐答案

WebDriver 绝对不会进行任何跟踪以检测哪个窗口实际上位于操作系统的前台,并且在打开新的浏览器窗口时不会自动切换.这意味着获取新打开的弹出窗口句柄的正确方法是一个多步骤过程.为此,您将:

WebDriver does absolutely no tracking whatsoever to detect which window is actually in the foreground in the OS, and does no automatic switching when new browser windows are opened. That means the proper way to get the handle of a newly-opened popup window is a multi-step process. To do so, you would:

  1. 将当前聚焦的窗口句柄保存到一个变量中,以便您可以稍后切换回.
  2. 获取当前打开的窗口句柄列表.
  3. 执行会导致新窗口出现的操作.
  4. 等待窗口句柄的数量增加 1.
  5. 获取新的窗口句柄列表.
  6. 在句柄列表中查找新句柄.
  7. 切换到那个新窗口.

在使用 .NET 语言绑定的代码中,这看起来像这样:

In code using the .NET language bindings, that would look something like this:

string currentHandle = driver.CurrentWindowHandle;
ReadOnlyCollection<string> originalHandles = driver.WindowHandles;

// Cause the popup to appear
driver.FindElement(By.XPath("//*[@id='webtraffic_popup_start_button']")).Click();

// WebDriverWait.Until<T> waits until the delegate returns
// a non-null value for object types. We can leverage this
// behavior to return the popup window handle.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
string popupWindowHandle = wait.Until<string>((d) =>
{
    string foundHandle = null;

    // Subtract out the list of known handles. In the case of a single
    // popup, the newHandles list will only have one value.
    List<string> newHandles = driver.WindowHandles.Except(originalHandles).ToList();
    if (newHandles.Count > 0)
    {
        foundHandle = newHandles[0];
    }

    return foundHandle;
});

driver.SwitchTo().Window(popupWindowHandle);

// Do whatever you need to on the popup browser, then...
driver.Close();
driver.SwitchTo().Window(currentHandle);

或者,如果您使用 .NET 绑定,WebDriver.Support 程序集中有一个 PopupWindowFinder 类,专门为您执行这些操作而设计.使用该类要简单得多.

Alternatively, if you're using the .NET bindings, there's a PopupWindowFinder class in the WebDriver.Support assembly that is specifically designed to do these operations for you. Using that class is much simpler.

// Get the current window handle so you can switch back later.
string currentHandle = driver.CurrentWindowHandle;

// Find the element that triggers the popup when clicked on.
IWebElement element = driver.FindElement(By.XPath("//*[@id='webtraffic_popup_start_button']"));

// The Click method of the PopupWindowFinder class will click
// the desired element, wait for the popup to appear, and return
// the window handle to the popped-up browser window. Note that
// you still need to switch to the window to manipulate the page
// displayed by the popup window.
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(element);

driver.SwitchTo().Window(popupWindowHandle);

// Do whatever you need to on the popup browser, then...
driver.Close();

// Switch back to parent window
driver.SwitchTo().Window(currentHandle);

这篇关于硒网络驱动程序中的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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