There are multiple ways to open a link in new tab in using Selenium WebDriver.
Code Block:
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NewTab_blank_iterator {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://mail.google.com/");
String firstWindowHandle = driver.getWindowHandle();
System.out.println("First Window Handle is: "+firstWindowHandle);
// Opening an adjacent blank tab
((JavascriptExecutor)driver).executeScript("window.open('','_blank');");
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allWindowHandles = driver.getWindowHandles();
// Using iterator
Iterator<String> itr = allWindowHandles.iterator();
while(itr.hasNext()) {
String nextWindow = itr.next();
if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) {
driver.switchTo().window(nextWindow);
System.out.println("New Tab Window Handle is: "+nextWindow);
}
}
}
}
Console Output:
First Window Handle is: CDwindow-0D89767363ED691767000F01E6712D0B
New Tab Window Handle is: CDwindow-7232D2058514ED22344F129D30A0CCE7
Browser Snapshot:
Code Block:
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NewTab_url_forLoop {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
String url1 = "https://mail.google.com/";
String url2 = "https://www.facebook.com/";
driver.get(url1);
String firstWindowHandle = driver.getWindowHandle();
System.out.println("First Window Handle is: "+firstWindowHandle);
// Opening Facebook in the adjacent TAB
((JavascriptExecutor)driver).executeScript("window.open('" + url2 +"');");
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allWindowHandles = driver.getWindowHandles();
// Using iterator
Iterator<String> itr = allWindowHandles.iterator();
while(itr.hasNext()) {
String nextWindow = itr.next();
if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) {
driver.switchTo().window(nextWindow);
System.out.println("New Tab Window Handle is: "+nextWindow);
}
}
}
}
Console Output:
First Window Handle is: CDwindow-01F5622275A2EA2C1ABE2F0CDEB3D09B
New Tab Window Handle is: CDwindow-9E3349B91FB2FA4D5B7D4A90D0E87BD3
Browser Snapshot: