How to handle multiple windows in Selenium?


Example of handling Multiple Windows in Selenium:

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("https://www.abc.com/");
driver.manage().window().maximize();
String Window1 = driver.getWindowHandle();
driver.findElement(By.linkText("Contact us Now")).click();
for (String Window2 : driver.getWindowHandles())
{
driver.switchTo().window(Window2);
}
driver.findElement(By.xpath("//*[@id='bigin']")).sendKeys("abcde");
Thread.sleep(5000);
driver.close();
driver.switchTo().window(Window1);
driver.getTitle();
Thread.sleep(3000);
driver.close();
}

For more info, Contact Us