How to handle multiple browser windows in Selenium Java?
IHUB Talent: Best Selenium with Java Training in Hyderabad with Live Internship
Looking to build a strong career in automation testing? IHUB Talent offers the best Selenium with Java training in Hyderabad, designed to turn you into a job-ready automation tester with practical, hands-on skills and real-world experience through a live internship program.
At IHUB Talent, the training is led by industry experts who bring in real-time scenarios and case studies to ensure you're learning what truly matters in today’s software testing landscape. The course covers Selenium WebDriver, Java programming basics to advanced concepts, TestNG, Maven, Jenkins, Git, and frameworks like POM and BDD (Cucumber) — everything you need to become a full-stack automation tester.
What makes IHUB stand out is the live internship program where students work on real-time projects with actual companies. This practical exposure gives you the confidence to apply your skills in a real environment and helps you build a professional portfolio that impresses recruiters.
Whether you're a fresher, manual tester, or a working professional looking to upskill, IHUB Talent’s job-oriented training with 1:1 mentorship, resume building, and interview prep is the right choice.
The Actions class in Selenium is used to build and perform complex user interactions like mouse movements, keyboard events, drag-and-drop, and other advanced gestures that go beyond simple clicks or typing.
In Selenium with Java, when your test opens more than one browser window or tab (for example, by clicking a link that opens a new tab), you need to switch the driver’s focus to the correct window before interacting with elements. Selenium provides window handles to manage this.
🔑 Key Methods
-
getWindowHandle()→ Returns the current window handle (unique ID). -
getWindowHandles()→ Returns aSet<String>of all open window handles. -
driver.switchTo().window(handle)→ Switches focus to the specified window.
📝 Step-by-Step Approach
-
Store the main window handle using
getWindowHandle(). -
Perform an action that opens a new window/tab.
-
Get all window handles with
getWindowHandles(). -
Loop through the handles and switch to the new one.
-
Perform actions in the child window.
-
Switch back to the main window if needed.
✅ Example Code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class HandleMultipleWindows {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Step 1: Store main window handle
String mainWindow = driver.getWindowHandle();
// Step 2: Click a link that opens new window
driver.findElement(By.id("openNewWindow")).click();
// Step 3: Get all window handles
Set<String> allWindows = driver.getWindowHandles();
// Step 4: Switch to child window
for (String window : allWindows) {
if (!window.equals(mainWindow)) {
driver.switchTo().window(window);
System.out.println("Child window title: " + driver.getTitle());
// Step 5: Perform actions in child window
driver.findElement(By.id("confirmButton")).click();
Thread.sleep(2000);
// Close child window
driver.close();
}
}
// Step 6: Switch back to main window
driver.switchTo().window(mainWindow);
System.out.println("Back to main window: " + driver.getTitle());
driver.quit();
}
}
⚡ Pro Tips
-
Always store the main window handle before switching.
-
Use
driver.close()to close only the child window, butdriver.quit()to close all. -
Use
Thread.sleep()only for demo purposes; in real projects use Explicit Waits.
Would you like me to also explain how to handle multiple child windows (more than 2 at once) with an example?
Read More
Comments
Post a Comment