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 below 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.
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.open('https://www.wikipedia.org/', '_blank');");
To use the JavascriptExecutor class you need to import org.openqa.selenium.JavascriptExecutor.
Leave a reply
Your email address will not be published. Required fields are marked*