我当前在 C# 中的代码打开一个窗口,然后在单击按钮后导航到指定的 URL.
My current code below in C# opens a window then navigates to the specified URL after a button click.
protected void onboardButton_Click(object sender, EventArgs e)
{
IWebDriver driver = new ChromeDriver();
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.Navigate().GoToUrl("http://www.google.com")
}
但我计划导航到的站点具有单点登录.如何在我的现有浏览器会话中打开一个新标签页并从那里导航?上面的代码好像不行.
But the site that I am planning to navigate to has single sign-on. How can I open a new tab in my existing browser session and navigate from there? The above code does not seem to work.
要处理新标签,您应该先切换到它.请尝试以下操作:
To handle new tab you should switch to it first. Try following:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("http://www.google.com")
您可能还需要切换回来:
Also you might need to switch back:
driver.SwitchTo().Window(driver.WindowHandles.First());
这篇关于使用 Selenium 在现有浏览器会话中打开一个新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!