How to Handle Multiple Windows Using Selenium


This Post Explains you ,How to handle Multiple Windows Using Selenium.This is so easy to handle.We use getWindowHandles method.

import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class sbionline
{
public static void main(String[] args) throws Exception
{
FirefoxDriver fd=new FirefoxDriver();
fd.get(“https://www.onlinesbi.com/”);
Thread.sleep(5000);
//click on login button
fd.findElement(By.xpath(“/html/body/div/div[5]/div/div/h3/a[2]”)).click();
Thread.sleep(3000);
fd.findElement(By.linkText(“About OnlineSBI”)).click();
Thread.sleep(3000);
//get all the windows handles(address)
Set ws=fd.getWindowHandles();
//iterate in the collection
Iterator itr=ws.iterator();

String w1=itr.next();
String w2=itr.next();
//focus to 2nd window
fd.switchTo().window(w2);
fd.findElement(By.xpath(“/html/body/p/input”)).click();
//focus to 1st window
fd.switchTo().window(w1);
//click continue to login
fd.findElement(By.xpath(“/html/body/div/table/tbody/tr/td/table/tbody/tr[4]/td[2]/a/img”)).click();
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s