获取Flipkart窗口句柄时抛出java.util.NoSuchElementException求助
Fixing java.util.NoSuchElementException with Selenium Window Handles
Hey there! Let's break down why you're hitting this error and how to fix it step by step.
What's causing the error?
The java.util.NoSuchElementException pops up because you’re calling k.next() twice, but your Set<String> FlipKart only holds one window handle at that moment. This usually happens for two main reasons:
- The click on "Mobiles & Tablets" isn’t actually opening a new window—it’s navigating within the current tab instead.
- Even if it does open a new window, your code tries to fetch the second handle before the browser has fully registered the new window.
How to fix it
Let’s adjust your code to handle both scenarios properly:
- Confirm if the click opens a new window
First, check how many window handles exist after the click. If it’s still 1, the navigation is happening in the same tab. - Use explicit wait for the new window to load
Implicit wait works for elements, but not for window handles. Instead, useWebDriverWaitto wait until the number of window handles becomes 2.
Here’s the revised code with these fixes:
package MoinPrograms; import java.util.Iterator; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; public class MouseOverActions { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.flipkart.com/"); // Wait for and close the login popup driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); driver.findElement(By.xpath("//button[@class='_2AkmmA _29YdH8']")).click(); // Store the original window handle before clicking String parentHandle = driver.getWindowHandle(); // Click on Mobiles & Tablets driver.findElement(By.xpath("//p[@class='_1AuloC' and text()='Mobiles & Tablets']")).click(); // Wait up to 10 seconds for a new window to open WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.numberOfWindowsToBe(2)); // Fetch all window handles now that the new window is loaded Set<String> windowHandles = driver.getWindowHandles(); Iterator<String> iterator = windowHandles.iterator(); // Safely iterate to get both handles String handle1 = iterator.next(); String handle2 = null; while (iterator.hasNext()) { handle2 = iterator.next(); } // Print the handles (with a check for the second handle) System.out.println("Parent Window Handle: " + handle1); if (handle2 != null) { System.out.println("New Window Handle: " + handle2); // Uncomment below to switch to the new window if needed // driver.switchTo().window(handle2); } else { System.out.println("No new window opened—navigation happened in the current tab."); } // Don't forget to close the driver when done // driver.quit(); } }
Key improvements here:
- Stored the parent handle first: Makes it easy to tell the original window apart from the new one.
- Explicit wait for window count: Ensures we only try to access the second handle after the new window is fully registered.
- Safe iteration: Uses a while loop to check for a second handle, avoiding the NoSuchElementException entirely.
- Added a fallback message: Tells you if the click didn’t open a new tab at all.
Bonus tips:
- Always test the element’s behavior manually first—click "Mobiles & Tablets" in your browser to see if it opens a new tab or stays in the current one.
- Try to avoid mixing implicit and explicit waits unless necessary; explicit waits are more reliable for specific conditions like window handles.
内容的提问来源于stack exchange,提问作者Moin Ahmed
相关产品推荐
相关产品推荐

