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.
How to Open a New Tab in Selenium. To open the new tab, use the same robot class code as created above. The only change here is that the code will click on the Returns and Orders link. In this case, the intent is to open one particular link (shown below) in a new tab as well as locate the link using Xpath.
We can interact with an existing browser session. This is performed by using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.
Although there are multiple ways of opening a new tab in Selenium like using Robot class, using Actions class, passing Keys. Control+”t” in the sendKeys() method to any element.
Just like you might open web pages in different tabs locally, it's also possible to have multiple tabs up in one browser during a Selenium test.
Sending Keys.Control + "t"
didn't work for me. I had to do it with javascript and then switch to it.
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With