web analytics
Google + RSS Feed

Multiple Window handle using selenium webdriver

0

January 21, 2014 by admin

Example 1:-

public class Handle_popupNAlert

{
public static void main(String[] args ) throws InterruptedException
{
WebDriver driver= new FirefoxDriver();
driver.get(“http://www.rediff.com/”);
WebElement sign = driver.findElement(By.xpath(“//html/body/div[3]/div[3]/span[4]/span/a”));
sign.click();

Set<String> windowId = driver.getWindowHandles(); // get window id of current window
Iterator<String> itererator = windowId.iterator();

String mainWinID = itererator.next();
String newAdwinID = itererator.next();

driver.switchTo().window(newAdwinID);
System.out.println(driver.getTitle());
Thread.sleep(3000);
driver.close();

driver.switchTo().window(mainWinID);
System.out.println(driver.getTitle());
Thread.sleep(2000);

WebElement email_id= driver.findElement(By.xpath(“//*[@id=’c_uname’]”));
email_id.sendKeys(“hi”);
Thread.sleep(5000);

driver.close();
driver.quit();
}
}

Example 2:  How can we handling browser pop up windows with selenium webdriver + Chrome?

public class PopUPandTabHandlingWithSET {

	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
		WebDriver myTestDriver = new ChromeDriver();
		myTestDriver.get("http://www.codeproject.com");
		// Create the set
		Set<String> codeprojectWindowID = myTestDriver.getWindowHandles();
		System.out.println("Main Window Handle ----------------- " + codeprojectWindowID);

		Iterator<String> it = codeprojectWindowID.iterator();

		while (it.hasNext()) {
		    // Get element
		    Object element = it.next();
		    System.out.println(element);
		}

		myTestDriver.findElement(By.xpath("//*[@id='C']/div[1]/div[3]/a[2]/img")).click();

		// Go to second window and click on forgot password link

		codeprojectWindowID = myTestDriver.getWindowHandles();
		it = codeprojectWindowID.iterator();
		String MainWindowHandle = it.next();
		String NewWindowHandle = it.next();
		System.out.println(MainWindowHandle);
		System.out.println(NewWindowHandle);

		Thread.sleep(3000L);

		myTestDriver.switchTo().window(NewWindowHandle);

		myTestDriver.findElement(By.xpath("//*[@id='link-forgot-passwd']")).click();

		// Go to 3rd popup window and click on Submit button

		codeprojectWindowID = myTestDriver.getWindowHandles();
		it = codeprojectWindowID.iterator();
		it.next();
		it.next();

		String NewPopUPWindow = it.next();

		myTestDriver.switchTo().window(NewPopUPWindow);

		myTestDriver.findElement(By.xpath("//input[@class='button g-button g-button-submit']")).click();

		// Close all Opened windows
		myTestDriver.close();

		myTestDriver.switchTo().window(NewWindowHandle);
		myTestDriver.close();

		myTestDriver.switchTo().window(MainWindowHandle);
		myTestDriver.close();

	}

}

0 comments »

Leave a Reply

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


nine − = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts