使用Selenium在现有浏览器会话中打开一个新选项卡 [英] Open a new tab in an existing browser session using Selenium

查看:580
本文介绍了使用Selenium在现有浏览器会话中打开一个新选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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")

还可能需要切换回去

driver.SwitchTo().Window(driver.WindowHandles.First());

这篇关于使用Selenium在现有浏览器会话中打开一个新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆