How to open a new tab using Selenium WebDriver? (C#)

How to open a new tab using Selenium WebDriver? (C#)

The following code shows how to open a link in a new tab using element.SendKeys(...) in the first example. This code more accurately models human behaviour compared to the following example as it directly models human behaviour.

IWebElement query = driver.FindElement(By.XPath("//a[strong[.='English']]"));
query.SendKeys(Keys.Control + Keys.Return);

 

In the second example it shows how to open an empty tab using the JavaScriptExecutor. This is less accurate when it comes to modelling human input, however for those familiar with javascript it is a very easy way to open multiple tabs at once as it does not depend on any elements present on the page.

 

IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
js.ExecuteScript("window.open('your URL', '_blank');");

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.