Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a new tab in an existing browser session using Selenium

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.

like image 584
JPaulPunzalan Avatar asked Jan 11 '17 09:01

JPaulPunzalan


People also ask

How do I go to a new tab in Selenium?

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.

Can Selenium interact with an existing browser session?

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.

How do I get an action class to open a new tab?

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.

Can Selenium open multiple tabs?

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.


1 Answers

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());
like image 106
nthpixel Avatar answered Nov 15 '22 19:11

nthpixel