最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How do I switch between multiple browser windows or tabs in Selenium with Java? - Stack Overflow

matteradmin6PV0评论

I am working with multiple tabs in a browser, but I can’t figure out how to switch from one window/tab to another. How can I correctly handle switching between multiple windows in Selenium WebDriver?

String originalWindow = driver.getWindowHandle();
    driver.findElement(By.id("openNewTab")).click();

    Set<String> windowHandles = driver.getWindowHandles();
    for (String windowHandle : windowHandles) {

    if (!windowHandle.equals(originalWindow)) {
        driver.switchTo().window(windowHandle);
     }
    }

I am working with multiple tabs in a browser, but I can’t figure out how to switch from one window/tab to another. How can I correctly handle switching between multiple windows in Selenium WebDriver?

String originalWindow = driver.getWindowHandle();
    driver.findElement(By.id("openNewTab")).click();

    Set<String> windowHandles = driver.getWindowHandles();
    for (String windowHandle : windowHandles) {

    if (!windowHandle.equals(originalWindow)) {
        driver.switchTo().window(windowHandle);
     }
    }
Share Improve this question edited Nov 19, 2024 at 4:11 Kefi Tech Solutions Pvt Ltd asked Nov 18, 2024 at 11:54 Kefi Tech Solutions Pvt LtdKefi Tech Solutions Pvt Ltd 112 bronze badges 1
  • Can you say what is wrong here? Do you have multiple window handles after "clicking" openNewTab? – matt Commented Nov 18, 2024 at 12:15
Add a comment  | 

1 Answer 1

Reset to default -1

When working with multiple windows in Selenium WebDriver, here’s how you can handle them step by step:

  1. First, save the original window’s handle using getWindowHandle(). This is important so you can switch back to it later.
  2. After opening a new window (like by clicking a button that opens a new tab), use getWindowHandles() to get all the currently open window handles.
  3. Then, use switchTo().window() to switch to the new window.

Here’s a simple example of how the code looks:

//Save the original window handle

String originalWindow = driver.getWindowHandle();


// Open a new window (e.g., by clicking a button)

driver.findElement(By.id("openNewTab")).click();


// Get all window handles

Set<String> windowHandles = driver.getWindowHandles();


// Switch to the new window


for (String windowHandle : windowHandles) {


    if (!windowHandle.equals(originalWindow)) {
        driver.switchTo().window(windowHandle);


        break;
    }
}

// Once done with the new window, you can switch back to the original window

driver.switchTo().window(originalWindow);
Post a comment

comment list (0)

  1. No comments so far