所以我在 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:
在使用 .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);更多推荐
硒网络驱动程序中的弹出窗口
发布评论