[英]Navigate driver to new opened window in selenium with C#
我编写了一个简单的代码来提交Selenium的注册表单。 在提交之前,驱动程序应该从主页来注册页面。
var firefox = new FirefoxDriver();
firefox.Navigate().GoToUrl("http://mywebsite/home");
如果我打印firefox.Title
,它会立即显示主页标题
在主页上,有一个注册按钮。 注册按钮的链接如下。
<a target="_blank" href="SignUp.jsp">Register Here</a>
为了导航到注册页面,我写了一行:
firefox.FindElement(By.CssSelector("a[href='SignUp.jsp']")).Click();
之后,驱动程序在firefox
浏览器的new window
中向我显示注册页面。 为了将驱动程序导航到注册,我编写了firefox.Navigate();
现在, 如果我打印firefox.Title
,它将再次显示主页标题 。
请帮我找出问题。 提前致谢。
由于您从未切换到新打开的窗口,因此您几乎抓住了相同的title
// 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);
并且,切换到新窗口后,您应该获得新标题。
但是,这个窗口处理过程完全让我感到困惑。 Selenium
.Net
绑定提供PopupWindowFinder类来处理窗口。
最后还要感谢JimEvans他漂亮的作品和这个
采用
firefox.SwitchTo().Window(handle);
其中handle
是firefox.WindowHandles
的实例之一。 这将在不同的窗口实例之间切换。 您可以在IWebDriver.SwitchTo()
的文档中找到更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.