Handle Windows Switching in Selenium Web-driver

Posted by

We can handle switching of windows in selenium web-driver using Switch To methods which will allow us to switch control from main window to sub/child window. In web applications most of time you must have faced this scenario where you must deal with multiple windows.

Script example how to handle multiple windows in selenium web-driver

  1. import java.util.Iterator;
  2. import java.util.Set;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.firefox.FirefoxDriver;
  6. import org.openqa.selenium.interactions.Actions;
  7. public class Swtich_window_session {
  8. public void TestingPopUp() throws InterruptedException {
  9. WebDriver driver=new FirefoxDriver();
  10.  driver.manage().window().maximize();
  11.  driver.get(“http://www.naukri.com/”);
  12.  String main_window=driver.getWindowHandle();
  13.  Set<String>s1=driver.getWindowHandles();
  14.  Iterator<String> I1= s1.iterator();
  15. while(I1.hasNext())  {
  16. String sub_window =I1.next();
  17.  if(!parent.equals(sub_window))  {
  18. driver.switchTo().window(sub_window);
  19. System.out.println(driver.switchTo().window(sub_window).getTitle());
  20.  driver.close();
  21. }  }
  22. driver.switchTo().window(main_window);
  23.  }}

Leave a Reply

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