Selenium WebDriver Switch Window Commands
Some Web application may have multiple windows & many frames. During Multiple window’s navigation selenium webdriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This id is called window’s handler. In simple terms, each unique window has a unique ID, so that Selenium can differentiate when it is switching controls from one window to the other.
· window handle command:
1. GetWindowHandle Command:
Purpose: To get the window handle of the current window.
String handle= driver.getWindowHandle();//Return a string of alphanumeric window handle
2. GetWindowHandles command:
Purpose: To get the window handle of all the current windows.
Set<String> handle= driver.getWindowHandles();//Return a set of window handle
3. SwitchTo Window Command:
Purpose: WebDriver supports moving between named windows using the “switchTo” method.
driver.switchTo().window("windowName");
Or
Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);}
Or
Switching between windows with Iterators:
driver.findElement(By.id(“id of the link which opens new window”)).click();
//wait till two windows are not opened
waitForNumberofWindowsToEqual(2);//this method is for wait
Set handles = driver.getWindowHandles();
firstWinHandle = driver.getWindowHandle(); handles.remove(firstWinHandle);
String winHandle=handles.iterator().next();
if (winHandle!=firstWinHandle){
//To retrieve the handle of second window, extracting the handle which does not match to first window handle
secondWinHandle=winHandle; //Storing handle of second window handle
//Switch control to new window
driver.switchTo().window(secondWinHandle);
Switch to Frames command
1. SwitchTo Frame Command:
Purpose: WebDriver supports moving between named frames using the “switchTo” method.
driver.switchTo().frame("frameName");
2. SwitchTo Popup Command:
Purpose: WebDriver supports moving between named PopUps using the “switchTo” method. With this object you can now accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, and prompts.
Alert alert = driver.switchTo().alert();
Switching Between Frames Example:
We ll navigate to : https://seleniumhq.github.io/selenium/docs/api/java/
This page has 3 frames whose "name" attributes are indicated above. We wish to access the "Deprecated" link encircled above in yellow. In order to do that, we must first instruct WebDriver to switch to the "classFrame" frame using the "switchTo().frame()" method. We will use the name attribute of the frame as the parameter for the "frame()" part.
and here is a sample code for your try:
package framesandMultipleWindows;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Frames {
public static void main(String[] args) {
// TODO Auto-generated method stub
//
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\HEMANT\\Desktop\\Softwares\\selenium-java-2.53.0\\geckodriver-v0.10.0-win64\\geckodriver.exe");
//Initiate firefox driver
driver = new FirefoxDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String baserUrl = "https://seleniumhq.github.io/selenium/docs/api/java/";
driver.get(baserUrl);
//Switch to package frame
driver.switchTo().frame("packageFrame");
//click on CoreTestCase.
driver.findElement(By.linkText("CoreTestCase")).click();
driver.close();
}
}
Switching between Windows Example
In this example we are using : https://www.w3schools.com/js/js_popup.asp
We’ll follow below steps :
· Click on try it yourself link under “ Confirm Box” Text.
· In the next Page – Click on “Try it” Button.
· Accept the alert.
And here is a code
package framesandMultipleWindows;
import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WindowsNavigationalert {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\HEMANT\\Desktop\\Softwares\\selenium-java-2.53.0\\geckodriver-v0.10.0-win64\\geckodriver.exe");
//Initiate firefox driver
driver = new FirefoxDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String baserUrl = "https://www.w3schools.com/js/js_popup.asp";
driver.get(baserUrl);
// Store and Print the name of the First window on the console
String handle= driver.getWindowHandle();
System.out.println(handle);
//Click on link
driver.findElement(By.xpath("https://*[@id='main']/div[6]/a")).click();
// Store and Print the name of all the windows open
Set handles = driver.getWindowHandles();
System.out.println(handles);
//passing the control to other window
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
System.out.println(tabs2);
driver.switchTo().window(tabs2.get(1));
driver.close();
//clicking on the button Try it
driver.findElement(By.xpath("https://button[contains(.,'Try it')]")).click();
// switch to alert and accept it
driver.switchTo().alert().accept();
}
}
this is all about Switch window commands. Let me know if any suggestions or questions.
#SwitchCommands # SwitchTo() #SeleniumWebdriver #Selenium3.0
Senior QA Engineer, Toddle
6 年(unamused) This is copied from toolsqa.com .. I came here for something that actually fixes my problem...?https://toolsqa.com/selenium-webdriver/switch-commands/ (unamused)
Test Architect- Selenium| Cucumber| Appium| Devops| AI-ML
7 年Try changing the stable version of geko driver it will work
Staff Software Engineer at ServiceNow
7 年The api getWindowHandle() always returns 2147483649 to me instead of an alpha numeric window handle. I'm using firefox 55, gecko 18, selenium 3.5.1. I'm sure it's a bug from firefox/gecko. Please correct me if I'm wrong