How to Handle Multiple Browser Tabs or Windows in Selenium
๐ How to Handle Multiple Browser Tabs or Windows in Selenium
In web automation, you might encounter scenarios where a link opens in a new tab or window. Selenium provides methods to switch between these windows so you can interact with the content.
✅ Key Methods to Know
driver.getWindowHandles()
Returns a list of all open window handles (IDs).
driver.getWindowHandle()
Returns the handle (ID) of the current window.
driver.switchTo().window(handle)
Switches to the specified window.
๐ Steps to Handle Multiple Windows/Tabs
Open the initial page.
Trigger action that opens a new tab/window.
Get window handles.
Switch to the new window.
Interact with the new page.
Switch back if needed.
๐งช Example in Selenium (Java)
java
Copy
Edit
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class WindowHandlingExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
// Step 1: Open initial page
driver.get("https://example.com");
// Step 2: Click something that opens a new tab/window
driver.findElement(By.linkText("Open New Tab")).click();
// Step 3: Get all window handles
String originalWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
// Step 4: Switch to the new window
for (String windowHandle : allWindows) {
if (!windowHandle.equals(originalWindow)) {
driver.switchTo().window(windowHandle);
break;
}
}
// Step 5: Perform actions in the new window
System.out.println("Title in new window: " + driver.getTitle());
// Step 6: Switch back to original window
driver.switchTo().window(originalWindow);
System.out.println("Back to original window: " + driver.getTitle());
driver.quit();
}
}
๐งช Example in Selenium (Python)
python
Copy
Edit
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# Click to open new window
driver.find_element("link text", "Open New Tab").click()
# Get all window handles
original = driver.current_window_handle
all_windows = driver.window_handles
# Switch to the new window
for window in all_windows:
if window != original:
driver.switch_to.window(window)
break
print("New window title:", driver.title)
# Switch back to original window
driver.switch_to.window(original)
print("Back to original window:", driver.title)
driver.quit()
๐ Tips
Always store the original window handle before switching.
You can close a specific tab using driver.close() and then switch back.
Wait for new tabs/windows if opened with a delay (use WebDriverWait).
๐ Summary
Action Selenium Command
Get current window driver.getWindowHandle()
Get all windows driver.getWindowHandles()
Switch window driver.switchTo().window(handle)
Close current window driver.close()
Learn Selenium Python Training in Hyderabad
Read More
Waits in Selenium: Implicit vs Explicit Waits Explained
Automating Form Filling Using Selenium and Python
Handling Alerts, Pop-ups, and iFrames in Selenium with Python
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment