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 = window.open(); w.document.open(); w.document.write('Open New Tab'); w.document.close();}; createDoc();");

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

// Go back to Parent Tab

driver.switchTo().window(parentHandle);

Advertisements