Open a new tab in an existing browser session using Selenium Open a new tab in an existing browser session using Selenium asp.net asp.net

Open a new tab in an existing browser session using Selenium


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


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


This may not works:

driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");

Alternative: Find clickable element with target blank (search for "blank" in page's surce code). This will open new tab.

Than switch between tabs (thanks @Andersson) with:

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