Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows.
package com.windowshandling;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Windows_Handle {
public static WebDriver d;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
d=new ChromeDriver();
d.navigate().to("https://www.facebook.com");
d.manage().window().maximize();
String mainwindow=d.getWindowHandle();
System.out.println("unique id for mainwindow:::"+mainwindow);
d.findElement(By.linkText("Terms")).click();
Set<String>windows=d.getWindowHandles();
for(String eachwindow:windows){
System.out.println("window id is:::"+windows);
if(!mainwindow.equals(eachwindow))
{
d.switchTo().window(eachwindow);
System.out.println("subwindow title is::"+d.getTitle());
d.findElement(By.xpath(".//*[@id='email']")).sendKeys("testing@gmail.com");
d.findElement(By.xpath(".//*[@id='pass']")).sendKeys("test12345");
d.findElement(By.xpath(".//*[@id='u_0_0']")).submit();
d.switchTo().window(mainwindow);
System.out.println("title is::"+d.getTitle());
d.findElement(By.xpath(".//*[@id='email']")).sendKeys("testing@gmail.com");
d.findElement(By.xpath(".//*[@id='pass']")).sendKeys("test12345");
d.findElement(By.xpath(".//*[@id='u_0_0']")).submit();
String text=d.findElement(By.xpath(".//*[@id='globalContainer']/div[3]/div/div/div/div[2]/a")).getText();
System.out.println("message is::"+text);
}
}
}
}
No comments:
Post a Comment