How do I manage browser windows and tabs using Selenium WebDriver?

Managing browser windows and tabs is an important aspect of automating web applications using Selenium WebDriver. Below are some of the key operations you might want to perform and how to handle them in both Python and JavaScript (Node.js) using Selenium.

Opening a New Tab or Window

Python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# Initialize the WebDriver instance
driver = webdriver.Chrome()

# Open a new window using JavaScript
driver.execute_script("window.open('');")

# Switch to the new window which is the second window
driver.switch_to.window(driver.window_handles[1])

# Navigate to a new URL in the new window
driver.get("https://www.example.com")

# Close the new window
driver.close()

# Switch back to the original window
driver.switch_to.window(driver.window_handles[0])

# Open a new tab using keyboard shortcut (Ctrl + t)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')

JavaScript (Node.js)

const { Builder, By, Key, until } = require('selenium-webdriver');

(async function example() {
    let driver = await new Builder().forBrowser('chrome').build();
    try {
        // Open a new tab using JavaScript
        await driver.executeScript('window.open("","_blank");');

        // Switch to the new tab which is the second tab
        let windows = await driver.getAllWindowHandles();
        await driver.switchTo().window(windows[1]);

        // Navigate to a new URL in the new tab
        await driver.get('https://www.example.com');

        // Close the new tab
        await driver.close();

        // Switch back to the original tab
        await driver.switchTo().window(windows[0]);

        // Open a new tab using keyboard shortcut (Ctrl + t)
        await driver.findElement(By.tagName('body')).sendKeys(Key.CONTROL + 't');
    } finally {
        // Quit the WebDriver session
        await driver.quit();
    }
})();

Switching Between Tabs or Windows

Python

# Assume driver is already initialized and multiple windows/tabs are open

# Get a list of all open windows/tabs
windows = driver.window_handles

# Switch to the second window/tab
driver.switch_to.window(windows[1])

# Perform operations in the new window/tab

# Switch back to the first window/tab
driver.switch_to.window(windows[0])

JavaScript (Node.js)

// Assume driver is already initialized and multiple windows/tabs are open

async function switchTabs(driver) {
    const windows = await driver.getAllWindowHandles();

    // Switch to the second window/tab
    await driver.switchTo().window(windows[1]);

    // Perform operations in the new window/tab

    // Switch back to the first window/tab
    await driver.switchTo().window(windows[0]);
}

Closing a Tab or Window

Python

# Close the current window/tab
driver.close()

# If you have closed a window/tab other than the last one, you need to switch to another one
if len(driver.window_handles) > 0:
    driver.switch_to.window(driver.window_handles[0])

JavaScript (Node.js)

// Close the current window/tab
await driver.close();

// If you have closed a window/tab other than the last one, you need to switch to another one
const windows = await driver.getAllWindowHandles();
if (windows.length > 0) {
    await driver.switchTo().window(windows[0]);
}

Maximizing or Resizing Browser Window

Python

# Maximize the current window
driver.maximize_window()

# Set a specific size for the window
driver.set_window_size(1024, 768)

JavaScript (Node.js)

// Maximize the current window
await driver.manage().window().maximize();

// Set a specific size for the window
await driver.manage().window().setRect({ width: 1024, height: 768 });

Keep in mind that managing windows and tabs might behave differently across different browsers and operating systems. Always ensure that your Selenium WebDriver is up to date and compatible with the browser version you are automating.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon