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

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

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

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
driver.findElement(By.xpath("//a[strong[.='English']]")).sendKeys(selectLinkOpeninNewTab);

 

In the code sample 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.

 

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.