79406765

Date: 2025-02-02 14:35:03
Score: 4.5
Natty:
Report link

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.

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Blacklisted phrase (1): StackOverflow
  • RegEx Blacklisted phrase (2.5): please share your code
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: AnujGupta