Webdriver技术问询:如何查找连续的前N个带flag-icon-gb类的元素
Fixing Your Webdriver Issue: Finding Consecutive GB Flag Tips
Hey there! Let's work through this problem step by step. Your goal is to find the first consecutive list of <li> elements with the GB flag, verify they match the first element's country code, and stop as soon as you hit a non-GB element. Let's break down what's off with your current code and fix it.
What's Wrong with Your Current Approach
- Skipping non-GB elements upfront: Using
findElementswith a selector targeting onlyflag-icon-gbspans pulls all GB elements regardless of their position. This means you can't check if they're consecutive—if there's a non-GB element in between, your list will still include GB elements after it, breaking the "consecutive" requirement. - Incorrect WebElement comparison:
WebElement.equals()checks if two elements are the exact same DOM object, not if they have the same country code. Since each<li>is a separate element, this will always returnfalseafter the first item, which is why your logic fails.
Fixed Solution
Instead of filtering first, we'll iterate through all <li> elements in order, check each one's flag class, and stop immediately when we hit a non-GB element. Here's the revised code:
// Get all <li> feed items in the order they appear on the page List<WebElement> allTips = driver.findElements(By.cssSelector("ul.feed-tips li.feed-item")); if (allTips.isEmpty()) { System.out.println("No tips found on the page"); return; } // Verify the first tip has the GB flag WebElement firstTip = allTips.get(0); WebElement firstFlag = firstTip.findElement(By.cssSelector("span.original-language-image")); boolean isFirstTipGB = firstFlag.getAttribute("class").contains("flag-icon-gb"); if (!isFirstTipGB) { System.out.println("First tip isn't a GB tip, stopping check"); return; } // Iterate through consecutive tips until a non-GB tip is found System.out.println("Processing consecutive GB tips:"); for (int i = 0; i < allTips.size(); i++) { WebElement currentTip = allTips.get(i); WebElement currentFlag = currentTip.findElement(By.cssSelector("span.original-language-image")); if (currentFlag.getAttribute("class").contains("flag-icon-gb")) { // Extract the tip link with a cleaner selector WebElement tipLink = currentTip.findElement(By.cssSelector("div.tip-list-row__tip div.tip-teams a")); System.out.println(String.format("GB Tip %d Link: %s", i+1, tipLink.getAttribute("href"))); } else { System.out.println("Encountered non-GB tip, stopping consecutive check"); break; } }
Key Improvements
- Enforces consecutive checks: We iterate through every
<li>in page order, so we stop immediately when we find a non-GB element. - Proper country code verification: Instead of comparing WebElement objects, we check the
classattribute of the flag span to confirm it's a GB tip—this correctly matches the first element's country code. - Cleaner selectors: Used CSS selectors instead of complex XPath for better readability and reliability.
- Edge case handling: Added checks for empty tip lists and non-GB first tips to prevent unexpected crashes.
内容的提问来源于stack exchange,提问作者Zoran
相关产品推荐
相关产品推荐

