Automation Testing
How to Open New Browser Tab in Selenuim
Answer − We can open a new browser tab in Selenium webdriver. The methods – Keys.chord and sendKeys are required to achieve this task. The Keys.chord method is used to send multiple keys at once.
We shall pass Keys.CONTROL and Keys.ENTER as parameters to this method. The complete string is again passed as a parameter to the sendKeys. Finally, the method sendKeys shall be applied on the link which we want to open in a new tab
Syntax
String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.xpath("//*[text()='Links']")). sendKeys(l);
Code Implementation
package RK1.Building_a_selenium_project;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
public class OpenNextTab {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "F:\\Work Environment\\MyProject\\QA_Round\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// wait of 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm");
// Keys.Chord string
String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
// open new tab
driver.findElement(By.xpath("//*[text()='Terms of Use']")).sendKeys(l);
driver.quit();
}
}


