public class SwitchToWindow {
public static void main(String[] args) {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver\\chromedriver_latest\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
String parent=driver.getWindowHandle();
Set<String> handles=driver.getWindowHandles();
String windows;
Iterator<String> I=handles.iterator();
while (I.hasNext()){
String childwindow= I.next();
if(driver.switchTo().window(childwindow).getTitle().equalsIgnoreCase("window title")||driver.switchTo().window(childwindow).getCurrentUrl().contains("url")){
driver.switchTo().window(childwindow);
}
}
for (String cw:handles)
{
if(driver.switchTo().window(cw).getTitle().contains("title")){
driver.switchTo().window(cw);
}
}
// Switch to specific window using index
ArrayList<String> allwindows=new ArrayList<>(driver.getWindowHandles());
int noofwindows=allwindows.size();
driver.switchTo().window(allwindows.get(2));
for (String winds:allwindows) {
if(driver.switchTo().window(winds).getTitle().contains("title")){
driver.switchTo().window(winds);
}
}
//Do actions in that window driver.close();
driver.switchTo().window(parent);
}
}
Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.