Open New Browser Tab in Selenuim

SeleniumAutomation TestingTesting Tools

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
public class OpenNewTab{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
      "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      // 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);
   }
}

Output


raja
Published on 29-Jun-2021 07:18:15
Advertisements