Cod*_*erz 12 c# selenium selenium-webdriver
所以我在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);
Run Code Online (Sandbox Code Playgroud)
任何帮助都将非常感谢谢谢
这就是弹出式的样子.
Jim*_*ans 27
WebDriver绝对没有任何跟踪来检测操作系统中哪个窗口实际位于前台,并且在打开新的浏览器窗口时不会自动切换.这意味着获取新打开的弹出窗口句柄的正确方法是一个多步骤的过程.为此,您将:
在使用.NET语言绑定的代码中,看起来像这样:
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.SwitchToWindow(currentHandle);
Run Code Online (Sandbox Code Playgroud)
或者,如果您正在使用.NET绑定,则PopupWindowFinder
WebDriver.Support程序集中有一个专门用于为您执行这些操作的类.使用该类更简单.
// 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);
Run Code Online (Sandbox Code Playgroud)
如果最后打开的窗口是您的目标,则只需在单击后执行以下操作
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());
Run Code Online (Sandbox Code Playgroud)
编辑
//You may need to go back to parent window to perform additional actions;
// to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());
// to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().First());
//or
driver.SwitchTo().DefaultContent();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33596 次 |
最近记录: |