Handling Multiple Windows in Selenium WebDriver

package multiplewindows;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class MultipleWins 
{
	@Test
	public void TestPopUp() throws InterruptedException
	{
		System.setProperty("webdriver.chrome.driver","E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
		
		WebDriver driver = new ChromeDriver();
		
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		driver.manage().window().maximize();
		
		driver.get("http://www.naukri.com");
		
		String parent = driver.getWindowHandle();
		
		Set<String>s = driver.getWindowHandles();
		
		Iterator<String> Iter = s.iterator();
		
		while(Iter.hasNext())
		{
			String childWindow=Iter.next();
			
			if(!parent.equals(childWindow))
			{
				driver.switchTo().window(childWindow);
				
				String childWinName = driver.switchTo().window(childWindow).getTitle();
				
				System.out.println("Child Window Title: ----"+childWinName+"\n");
				
				driver.close();
			}
		}
		 
		driver.switchTo().window(parent);
		
		driver.close();
		 
	}
}

 

One comment

  • Wow, the awesome article with the very informative post like read once a day for latest and useful stuff which helps more for my future growth. Thanks for sharing…

Leave a Reply

Your email address will not be published. Required fields are marked *