If you wants to work with multiple tabs then view THIS POST and wants to work with multiple IFrames then view THIS POST.
WebDriver.getWindowHandles()
In WebDriver software testing tool, We can use “WebDriver.getWindowHandles()” to get the handles of all opened windows by webdriver and then we can use that window handle to switch from from one window to another window. Example Syntax for getting window handles is as bellow.
Set<String> AllWindowHandles = driver.getWindowHandles();
WebDriver.switchTo().window()
WebDriver.switchTo().window() method is useful to switch from one window to another window of software web application. Example syntax is as bellow.
driver.switchTo().window(window2);
Bellow given webdriver example of switching window will explain you how selenium multi browser testing can be done. Execute it in your eclipse and try to understand how webdriver do it.
Copy bellow given @Test method part of handling multiple windows of webdriver and replace it with the @Test method part of example given on THIS PAGE. to check how multi browser testing using selenium works.(Note : @Test method is marked with pink color in that linked page).
Multiple browser in selenium test
@Test
public void test () throws InterruptedException
{
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();
// Get and store both window handles in array
Set<String> AllWindowHandles = driver.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
String window2 = (String) AllWindowHandles.toArray()[1];
System.out.print("nwindow2 handle code = "+AllWindowHandles.toArray()[1]);
//Switch to window2(child window) and performing actions on it.
driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
driver.findElement(By.xpath("//input[@value='Bike']")).click();
driver.findElement(By.xpath("//input[@value='Car']")).click();
driver.findElement(By.xpath("//input[@value='Boat']")).click();
driver.findElement(By.xpath("//input[@value='male']")).click();
Thread.sleep(5000);
//Switch to window1(parent window) and performing actions on it.
driver.switchTo().window(window1);
driver.findElement(By.xpath("//option[@id='country6']")).click();
driver.findElement(By.xpath("//input[@value='female']")).click();
driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
driver.switchTo().alert().accept();
Thread.sleep(5000);
//Once Again switch to window2(child window) and performing actions on it.
driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[@name='fname']")).clear();
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("Name Changed");
Thread.sleep(5000);
driver.close();
//Once Again switch to window1(parent window) and performing actions on it.
driver.switchTo().window(window1);
driver.findElement(By.xpath("//input[@value='male']")).click();
Thread.sleep(5000);
}
16 thoughts on “Example of Handling Multiple Browser Windows in Selenium WebDriver”
can you explain this syntax please?
et AllWindowHandles = driver.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
String window2 = (String) AllWindowHandles.toArray()[1];
System.out.print("nwindow2 handle code = "+AllWindowHandles.toArray()[1]);
i dont get any option to select the xpath for child window. how to inspect the element in child window with firebug??
Taking a set of String values named with AllWindowHandles By method getWindowHadles(), Assingning numbers to the new windows from the String values starting from zero
then by using that numbers we can switch to that particular windows,,,
Taking a set of String values named with AllWindowHandles By method getWindowHadles(), Assingning numbers to the new windows from the String values starting from zero
then by using that numbers we can switch to that particular windows,,,
Hi,
driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();
I'm unable to understand this xpath, actually couldn't understand how to create new instance of browser window? can someone please elaborate this?
You can use a website where on clicking a link/button a new window will open, for learning the windowhandle concept.
Eg: http://www.salesforce.com/in
driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();Could you please elobrate?WebDriver driver = new FirefoxDriver();driver.manage().window().maximize();driver.get();driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();Getting the below error:Unable to locate element: {"method":"xpath","selector":"//b[contains(.,'Open New Page')]"}Please suggest
You have used wrong page URL.Use driver.get();
Hi,
I have a scenario where I am getting one more window when I click an element in the second window so could you please let me know how to handle windows when it is launched in the middle or after assigning the windowhandle at the beginning
Hi,
My question when I am working on second window, I get one more window opened so how to handle this. Because we assign the windowhandles at the beginning so if you get one more window in the middle how to handle that. Please help me out.
Set AllWindowHandles = driver.getWindowHandles();
Why only Set collection we are using why not other collection. As we all know all the windows have unique Ids so we will not have any questions of duplicates values in set.
can any one give some websites have muliple frames in single window
Element is not clickable , it would click someother element. This error is thrown.
Using below method we can handle as much window as per need. just pass window number from navigation starting point i.e from parent window.
public static void switchToNewWindow(int windowNumber) {
Set allWindowHandles =driver.getWindowHandles();
Iterator itr=allWindowHandles.iterator();
int i=1;
while(itr.hasNext() && i < 10) {
String popupHandle= itr.next().toString();
driver.switchTo().window(popupHandle);
actualTitle= driver.getTitle();
System.out.println("Window title is :" + actualTitle);
if(i== windowNumber) break;
i++;
}
I have a question in form page click on one link it opens 30 windows so how to switch 28 window and doing some actions in that window
provide the output with diagrams for all the ending program to know how it looks like..?