Friday 11 November 2016

How to switch window using selenium webdriver

Hello

Today we will be learning how we can switch a window in selenium webdriver from one window to another.

For this we use a java code

                                     driver.switchTo().window(parentWindow);

where parentWindow is a parameter that is passed as a String in which the path of parent window is stored.To save a parent window path or a window path we use

                                   String parentWindow = driver.getWindowHandle().toString();

By using this we can switch window in selenium webdriver.We can switch to multiple window by saving the path of each window in a String and then passing it as a parameter in window() function



Script for switching window


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class SwitchWin 
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("any website name");
String parentWindow = driver.getWindowHandle().toString();
driver.findElement(By.name("click")).click();
driver.switchTo().window("PopupWindow");
driver.close();
driver.switchTo().window(parentWindow);
driver.close();

}
}

0 comments: