如何在Selenium WebDriver中保存搜索元素至ArrayList并查找指定元素
Got it, let's break this down properly—dealing with dynamic Amazon search results in Selenium requires a mix of handling lazy loading, safely extracting elements, and then using an ArrayList for easy manipulation. Here's a practical, step-by-step solution:
Amazon doesn't load all products at once; you need to scroll to the bottom repeatedly until no new items load. This ensures we capture every visible product in the search results.
// Initialize your WebDriver (ChromeDriver example) WebDriver driver = new ChromeDriver(); driver.get("https://www.amazon.com/"); driver.manage().window().maximize(); // Perform search first WebElement searchBox = driver.findElement(By.id("twotabsearchtextbox")); searchBox.sendKeys("wireless headphones"); searchBox.submit(); // Wait for initial results to load WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div[data-component-type='s-search-result']"))); // Scroll to load all dynamic results JavascriptExecutor js = (JavascriptExecutor) driver; List<WebElement> initialProducts = driver.findElements(By.cssSelector("div[data-component-type='s-search-result']")); int previousCount = 0; // Keep scrolling until no new products are loaded while (initialProducts.size() != previousCount) { previousCount = initialProducts.size(); // Scroll to the bottom of the page js.executeScript("window.scrollTo(0, document.body.scrollHeight);"); // Wait for new elements to load (adjust wait time based on page speed) wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("div[data-component-type='s-search-result']"), previousCount)); initialProducts = driver.findElements(By.cssSelector("div[data-component-type='s-search-result']")); }
Now that we have all loaded product elements, we can store them in an ArrayList<WebElement> for easy access and traversal.
ArrayList<WebElement> productList = new ArrayList<>(initialProducts); System.out.println("Total products captured: " + productList.size());
With the list in place, we can iterate through it to find specific products—like items with a certain brand name, price range, or keyword in the title.
Example: Find products with "Bose" in the title
String targetBrand = "Bose"; WebElement targetProduct = null; for (WebElement product : productList) { try { WebElement productTitle = product.findElement(By.cssSelector("h2 a span")); String title = productTitle.getText(); if (title.contains(targetBrand)) { targetProduct = product; System.out.println("Found target product: " + title); break; } } catch (NoSuchElementException e) { // Skip products with missing title elements continue; } } // Do something with the found product (e.g., click it) if (targetProduct != null) { targetProduct.findElement(By.cssSelector("h2 a")).click(); } else { System.out.println("No product matching '" + targetBrand + "' found."); }
- Skip fixed sleeps where possible: Instead of
Thread.sleep, useWebDriverWaitto wait for the number of products to increase—this makes the script more reliable across different network speeds. - Validate selectors: Amazon's DOM can change, so double-check that selectors like
div[data-component-type='s-search-result']work for your session. - Handle edge cases: Wrap element lookups in try-catch blocks to skip products with missing elements (like titles or prices) that might break the loop.
内容的提问来源于stack exchange,提问作者Abhishek Gaur

