Sunday 10 July 2022

How To Switch Windows in Selenium Java | How To Handle Window in Selenium Webdriver

                  In this post, we will see How To Switch Windows in Selenium Java | How To Handle Window in Selenium Webdriver 


Program Code (SwitchWindows.java)

package main;


import java.time.Duration;

import java.util.Iterator;

import java.util.Set;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class SwitchWindows {


public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver_win32\\chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.get("https://www.irctc.co.in/nget/train-search");

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

driver.findElement(By.xpath("//button[@aria-label='Confirmation. Get your favourite food at your train seat through <a class=\"txt-msg-info\" target=\"_blank\" href=\"https://www.ecatering.irctc.co.in/\">e-Catering</a> available at selected stations.<br/><br/>COVID 19 Alert:<br/><br/><i class=\"fa fa-circle\" ></i> All passengers are advised to wear face covers/masks at the entry/exit and during travel.<br/><br/><i class=\"fa fa-circle\" ></i> All passengers are advised that on arrival at their destination, the travelling passenger shall follow the Covid protocol/guidelines issued by Ministry of Home affairs, Ministry of Health and Family Welfare and concerned State/UTs on the subject from time to time.<br/><br/><i class=\"fa fa-circle\" ></i><a class=\"txt-msg-info\"target=\"_blank\"href=\"http://contents.irctc.co.in/en/CovidVaccinationInfoEng.pdf\"> Information on Covid 19 Vaccination Programme</a>. Press OK to confirm']")).click();

driver.findElement(By.xpath("//span[@class='allcircle circletwo']")).click();

Set<String> handles=driver.getWindowHandles(); //parentid,childid,subchildid

Iterator it=handles.iterator();

String parentid=(String) it.next();

String childid=(String) it.next();

driver.switchTo().window(childid);

driver.findElement(By.xpath("//div[@class='Nav-icons BusTickets']")).click();


//driver.switchTo().window(parentid);

}



Watch following video:


Watch on YouTube: https://www.youtube.com/watch?v=oywjm-W3wac

1 comment: