Java Selenium WebDriver:手动填充WebElement列表及元素迭代咨询
Hey there! Let's tackle your two main questions one by one—manually populating a List<WebElement> without duplicates, and iterating through elements like an element.next() method.
1. 手动填充List并避免重复
Since your page loads elements incrementally, you can't grab all elements in one go. The key here is to check if an element already exists in your list before adding it. But a quick heads-up: using List.contains() directly might not work reliably if the page refreshes or re-renders elements (since WebElement references can change even for the same DOM element). Instead, use a unique attribute of the element (like href for links) to verify duplicates.
Here's a Java example with Selenium:
import java.util.ArrayList; import java.util.List; import org.openqa.selenium.WebElement; import org.openqa.selenium.By; // Initialize your list List<WebElement> elementList = new ArrayList<>(); // Example: Get a new element (adjust the XPath to match your use case) WebElement currentElement = driver.findElement(By.xpath(".//a[starts-with(@href, 'your-target-pattern')]")); // Check if the element already exists in the list using a unique attribute boolean elementExists = elementList.stream() .anyMatch(existingEl -> existingEl.getAttribute("href").equals(currentElement.getAttribute("href"))); // Add only if it's not already present if (!elementExists) { elementList.add(currentElement); }
If you're dealing with scroll-loaded content, you'll need to combine this with scrolling to load new elements (more on that below).
2. 类似element.next()的元素迭代方式
There's no built-in element.next() method in Selenium, but you can use XPath axes or loop logic to iterate through elements sequentially.
方法A: 使用XPath的following-sibling轴(适用于兄弟元素)
If your target <a> elements are siblings under the same parent, use the following-sibling axis to grab the next matching element:
// Start with the first element WebElement current = driver.findElement(By.xpath(".//a[starts-with(@href, 'your-target-pattern')][1]")); while (current != null) { // Process the current element (add to list, etc.) boolean exists = elementList.stream() .anyMatch(el -> el.getAttribute("href").equals(current.getAttribute("href"))); if (!exists) { elementList.add(current); } // Try to find the next sibling element try { current = current.findElement(By.xpath("following-sibling::a[starts-with(@href, 'your-target-pattern')][1]")); } catch (org.openqa.selenium.NoSuchElementException e) { // No more elements to iterate current = null; } }
方法B: 滚动加载+循环(适用于无限滚动页面)
For pages that load new elements when scrolling to the bottom, combine scrolling with element detection:
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; WebDriverWait wait = new WebDriverWait(driver, 10); JavascriptExecutor js = (JavascriptExecutor) driver; int previousSize = 0; do { previousSize = elementList.size(); // Scroll to the bottom of the page to load new elements js.executeScript("window.scrollTo(0, document.body.scrollHeight);"); // Wait for new elements to load (adjust the XPath as needed) wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//a[starts-with(@href, 'your-target-pattern')]"))); // Grab all visible elements and add non-duplicates to the list List<WebElement> newElements = driver.findElements(By.xpath(".//a[starts-with(@href, 'your-target-pattern')]")); for (WebElement el : newElements) { boolean exists = elementList.stream() .anyMatch(existingEl -> existingEl.getAttribute("href").equals(el.getAttribute("href"))); if (!exists) { elementList.add(el); } } } while (elementList.size() > previousSize); // Stop when no new elements are added
This loop keeps scrolling and adding new elements until the list stops growing—meaning no more elements are loaded.
内容的提问来源于stack exchange,提问作者Ifloop

