How to handle multiple windows in Selenium?
Last Updated :
16 Nov, 2023
Sometimes when we click on a particular web element it opens a new window. To locate the web elements on the new window webpage, we need to shift the focus of selenium from the current page (main page) to the new page. In this article, we will try to shift the focus of selenium from one window to another new window. Here we will use the Chrome browser for which we require ChromeDriver you can download it from the official site of Selenium. We will use the geeksforgeeks.org website for automaChromeon practice.
Steps:
- Visit the official site of geeksforgeeks.org
- Click on courses at GeeksforGeeks from quick links
- Select a particular course ( Open with a new window )
- Click on the element of the new window
Expected Result: It should click on the element of the new window
Implementation:
Program 1: Click on the element of the new window without shifting the focus of selenium
Java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GFGWindowHandling {
public static void main(String[] args) throws InterruptedException
{
System.setProperty( "webdriver.chrome.driver" , "Path of chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get( "https:// www.geeksforgeeks.org/" );
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
JavascriptExecutor je = (JavascriptExecutor)driver;
je.executeScript( "window.scrollBy(0, 200)" );
driver.findElement(By.xpath( "(// span[text()='Courses at GeeksforGeeks'])[2]" )).click();
Thread.sleep( 2000 );
driver.findElement(By.xpath( "(// h4[text()='Data Structures and Algorithms - Self Paced'])[1]" )).click();
driver.findElement(By.xpath( "(// a[text()='(Read more here)'])[1]" )).click();
System.out.println( "operation is performed on new window" );
}
}
|
Console Output:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:”xpath”, “selector”:”(//a)[1]”}
This exception is because when we click on a particular course new window (child window) is open and when we try to click on Read more here, at that time focus of selenium is on the main window (parent window). So we will use some method to shift the focus of selenium from one window to another.
Syntax:
driver.switchTo().window(ID);
Where ID: ID of a particular window on which you need the change the focus of selenium.
To get the ID of Windows we will use the following method.
- driver.getWindowHandle(): To get the ID of the parent window (main window)
- driver.getWindowHandles(): To get the ID of child windows (new window)
Let’s observe how the IDs of two different windows are different,
Program 2: To get the IDs of different windows
Java
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GFGIDsOfWindows {
public static void main(String[] args) throws InterruptedException
{
System.setProperty( "webdriver.chrome.driver" , "Path of chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get( "https:// www.geeksforgeeks.org/" );
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
JavascriptExecutor je = (JavascriptExecutor)driver;
je.executeScript( "window.scrollBy(0, 200)" );
driver.findElement(By.xpath( "(// span[text()='Courses at GeeksforGeeks'])[2]" )).click();
Thread.sleep( 2000 );
driver.findElement(By.xpath( "(// h4[text()='Data Structures and Algorithms - Self Paced'])[1]" )).click();
String Parent_id = driver.getWindowHandle();
System.out.println(Parent_id);
Set<String> Child_id = driver.getWindowHandles();
for (String a : Child_id) {
System.out.println(a);
}
}
}
|
Console Output:
Here you can observe the IDs of windows are different.
CDwindow-EA925E71098EEFBB80858BE787CED1A5 (ID of main window)
CDwindow-C9078346729F1D0CF8AF12E938CE49DD (ID of new window)
So to change the focus of selenium from one window to another window we will use For each loop and provide the if-else condition. For each loop
for( Datatype variable : collection )
{
Statements;
}
Program 3: Click on the element of the new window by shifting the focus of selenium
Java
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GFGWindowHandlingEx {
public static void main(String[] args) throws InterruptedException
{
System.setProperty( "webdriver.chrome.driver" , "Path of chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get( "https:// www.geeksforgeeks.org/" );
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
JavascriptExecutor je = (JavascriptExecutor)driver;
je.executeScript( "window.scrollBy(0, 200)" );
driver.findElement(By.xpath( "(// span[text()='Courses at GeeksforGeeks'])[2]" )).click();
Thread.sleep( 2000 );
driver.findElement(By.xpath( "(// h4[text()='Data Structures and Algorithms - Self Paced'])[1]" )).click();
String Parent_id = driver.getWindowHandle();
System.out.println(Parent_id);
Set<String> Child_id = driver.getWindowHandles();
for (String a : Child_id) {
System.out.println(a);
if (Parent_id.equals(a)) {
}
else {
driver.switchTo().window(a);
Thread.sleep( 2000 );
driver.findElement(By.xpath( "// button[text()='Got it!']" )).click();
je.executeScript( "window.scrollBy(0, 600)" );
driver.findElement(By.xpath( "(// a[text()='(Read more here)'])[1]" )).click();
System.out.println( "operation is performed on new window" );
}
}
}
}
|
Console Output:
Output Video
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...