Selenium : Opening a new Tab in browser (Firefox , Chrome or, Safari)

// Get Handle of current window/Tab
String parentHandle = driver.getWindowHandle();

// Get JavaExecuter instance from driver
JavascriptExecutor j = (JavascriptExecutor)driver;

// Open a new Tab with text on it as "Open New Tab
j.executeScript("function createDoc(){var w =;; w.document.write('Open New Tab'); w.document.close();}; createDoc();");

// Go to the newly opened window
for (String winHandle : driver.getWindowHandles()) {
Do your work

// Go back to Parent Tab