弹出窗口的硒webdrivers [英] popup's in selenium webdrivers

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

问题描述

所以,我在C#中的winform硒火狐webdrivers工作,我有低于这个code获取显示,当你点击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);

pciated

任何帮助,这将是非常美联社$ P $谢谢

Any help with this would be much appreciated thank you

这是什么弹出的模样。

http://i60.tinypic.com/2ujmg08.png

推荐答案

的webdriver并绝对没有任何的跟踪检测的窗口实际上是在OS的前景,并不做任何自动切换时,新的浏览器窗口打开。这意味着,适当的方式来获得一个新打开的弹出窗口的句柄是一个多步骤的过程。要做到这一点,您可以:

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语言绑定code,这将是这个样子:

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.CurrentWindowHandles.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.SwitchToWindow(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();
driver.SwitchToWindow(currentHandle);

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

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