C# 使用C将驱动程序导航到selenium中新打开的窗口#

C# 使用C将驱动程序导航到selenium中新打开的窗口#,c#,firefox,selenium-webdriver,C#,Firefox,Selenium Webdriver,我编写了一个简单的代码,用Selenium提交一个注册表单。提交前,驱动程序应来自主页注册页面 var firefox = new FirefoxDriver(); firefox.Navigate().GoToUrl("http://mywebsite/home"); 如果我打印firefox.Title,它会显示当前主页的标题 在主页上,有一个注册按钮。注册按钮链接如下 <a target="_blank" href="SignUp.jsp">Register Here<

我编写了一个简单的代码,用Selenium提交一个注册表单。提交前,驱动程序应来自主页注册页面

var firefox = new FirefoxDriver();
firefox.Navigate().GoToUrl("http://mywebsite/home");
如果我打印
firefox.Title
,它会显示当前主页的标题

在主页上,有一个注册按钮。注册按钮链接如下

<a target="_blank" href="SignUp.jsp">Register Here</a>
之后,驱动程序在
firefox
浏览器的
new窗口中向我显示注册页面。为了将驱动程序导航到注册,我编写了
firefox.navigate()

现在如果我打印
firefox.Title
,它会再次显示主页的标题

请帮我找出问题所在。提前感谢。

使用

firefox.SwitchTo().Window(handle);

其中
handle
是在
firefox.WindowHandles
中找到的实例之一。这将在不同的窗口实例之间切换。您可以在文档中找到更多信息。

您几乎抓住了相同的
标题
,因为您从未切换到新打开的窗口

// 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
绑定提供了处理窗口的类

感谢JimEvans的优秀作品和

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