Switch Between 3 Tabs Using Selenium WebDriver with Java - Stack Overflow

admin2025-04-16  4

I'm trying to switch between three tabs using the Selenium getWindowHandles method and an iterator. It works well for two tabs, but when I add a third one, I encounter an error.

My goal is to start in Google, then switch to Facebook, then to StackOverflow, and finally switch back to Google. How can i fix this?

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:758)
    at java.base/java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:778)
    at locators.SwitchWindowTab.main(SwitchWindowTab.java:29)

I'm trying to switch between three tabs using the Selenium getWindowHandles method and an iterator. It works well for two tabs, but when I add a third one, I encounter an error.

My goal is to start in Google.com, then switch to Facebook.com, then to StackOverflow.com, and finally switch back to Google.com. How can i fix this?

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:758)
    at java.base/java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:778)
    at locators.SwitchWindowTab.main(SwitchWindowTab.java:29)
Share edited Feb 1 at 20:37 JeffC 26k5 gold badges34 silver badges55 bronze badges asked Feb 1 at 19:11 amalamal 3,60210 gold badges31 silver badges45 bronze badges 2
  • 1 Please add a minimal reproducible example. – mwopitz Commented Feb 1 at 19:52
  • If you mean browser tabs (different websites) please add that to your question, my first thought on your question was: so many ways to make a website show tabs, which one is meant?. And please also include a snippet of code how you work in Selenium (its basically the request of a mre but maybe not a complete runnable thingy is necessary) – cyberbrain Commented Feb 1 at 21:24
Add a comment  | 

1 Answer 1

Reset to default 2

You're encountering a NoSuchElementException because the Iterator is exhausted before switching to the third tab. Instead, use a List to store getWindowHandles() and access tabs by index.

You can refer the below code:

    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com");

    // Open new tabs
    driver.switchTo().newWindow(WindowType.TAB).get("https://www.facebook.com");
    driver.switchTo().newWindow(WindowType.TAB).get("https://stackoverflow.com");

    // Store window handles in a list
    Set<String> handles = driver.getWindowHandles();
    List<String> tabs = new ArrayList<>(handles);

    // Switch between tabs using index
    driver.switchTo().window(tabs.get(1)); // Facebook
    driver.switchTo().window(tabs.get(2)); // StackOverflow
    driver.switchTo().window(tabs.get(0)); // Back to Google

    driver.quit();

NOTE: If this code still doesn't work for you, please share your code so we can provide an exact and accurate solution.

转载请注明原文地址:http://anycun.com/QandA/1744818049a88037.html