gpt4 book ai didi

c# - Selenium 网络驱动程序中的弹出窗口

转载 作者:可可西里 更新时间:2023-11-01 08:16:09 26 4
gpt4 key购买 nike

所以我在 c# winform 中使用 selenium firefox webdrivers,我在下面有这段代码来获取当您单击“webtraffic_popup_start_button”时显示的弹出窗口的句柄,它应该获取弹出窗口的句柄,但弹出窗口句柄与当前句柄相同。

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);

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

这是弹出窗口的样子。

Popup_Image

最佳答案

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

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

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

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 绑定(bind),WebDriver.Support 程序集中有一个 PopupWindowFinder 类,专门为您执行这些操作而设计。使用该类要简单得多。

// 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);

关于c# - Selenium 网络驱动程序中的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27608921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com