Search code examples
javaselenium3

How to open new tab in Selenium 3 in java


Before tagging this as duplicate. Please read the Question. i have seen many answers for this kind of question. But none of them really worked.

System.setProperty("webdriver.gecko.driver", "src/test/driver/geckodriver.exe");
WebDriver driver = new FirefoxDriver();

driver.get("http://google.com");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
driver.get("https://www.facebook.com");

this my code. When I run this instead of opening a new tab its just opening in Current tab.How can i open second link in new tab?


Solution

  • You can use javascript for this in ur selenium code :-

      JavascriptExecutor js = (JavascriptExecutor) driver; 
     js.executeScript("window.open('https://www.google.com','_blank');");