Selenium - Window Handle
Selenium - Window Handle
GetWindowHandle Command
Purpose: To get the window handle of the current window.
String handle= driver.getWindowHandle();//Return a string of alphanumeric wind
ow handle
String handle= driver.getWindowHandle();//Return a string of alphanumeric wind
ow handle
GetWindowHandles Command
Purpose: To get the window handle of all the current windows.
Set<String> handle= driver.getWindowHandles();//Return a set of window handle
Set<String> handle= driver.getWindowHandles();//Return a set of window handle
SwitchTo Window Command
Purpose: WebDriver supports moving between named windows using the
.
switchTo
method
driver.switchTo().window("windowName");
driver.switchTo().window("windowName");
Or
Alternatively, you can pass a window handle to the switchTo().window()
ng this, it s possible to iterate over every open window like so:
method. Knowi
switchTo method.
driver.switchTo().frame("frameName");
driver.switchTo().frame("frameName");
SwitchTo PopUp Command
Purpose: WebDriver supports moving between named PopUps using the switchTo method.
After you ve triggered an action that opens a popup, you can access the alert and
it will return the currently open alert object. With this object you can now ac
cept, dismiss, read its contents or even type into a prompt. This interface work
import org.openqa.selenium.firefox.FirefoxDriver;
public class PracticeSwitchWindow {
public static WebDriver driver;
public static void main(String[] args) {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Put an Implicit wait, this means that any search for elements
on the page could take the time the implicit wait is set for before throwing ex
ception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL
driver.get("http://toolsqa.wpengine.com/automation-practice-swit
ch-windows/");
// Store and Print the name of the First window on the console
String handle= driver.getWindowHandle();
System.out.println(handle);
// Click on the Button "New Message Window"
driver.findElement(By.name("New Message Window")).click();
// Store and Print the name of all the windows open
Set handles = driver.getWindowHandles();
System.out.println(handles);
// Pass a window handle to the other window
for (String handle1 : driver.getWindowHandles()) {
System.out.println(handle1);
driver.switchTo().window(handle1);
}
// Closing Pop Up window
driver.close();
// Close Original window
driver.quit();
}
}
----------------------package practiceTestCases;
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.firefox.FirefoxDriver;
public class PracticeSwitchWindow {
public static WebDriver driver;
public static void main(String[] args) {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Put an Implicit wait, this means that any search for elements
on the page could take the time the implicit wait is set for before throwing ex
ception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL
driver.get("http://toolsqa.wpengine.com/automation-practice-swit
ch-windows/");
// Click on the Button "Alert Box"
driver.findElement(By.name("Alert Box")).click();
// Switch to JavaScript Alert window
Alert myAlert = driver.switchTo().alert();
// Accept the Alert
myAlert.accept();
// Close Original window
driver.close();
}
}
-----------------------------------