You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java Selenium统计谷歌搜索结果数并断言分页结果数一致?

实现思路

  • 初始化ChromeDriver,访问谷歌搜索主页
  • 输入关键词"selenium"并提交搜索
  • 等待第一页搜索结果加载,过滤掉<a>标签的结果后统计数量
  • 切换到第二页,重复上述统计操作
  • 使用断言验证两页的结果数量是否一致
  • 最后关闭浏览器驱动

完整示例代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import static org.junit.Assert.assertEquals;
import java.time.Duration;
import java.util.List;

public class GoogleSearchResultChecker {
    public static void main(String[] args) {
        // 若未将ChromeDriver配置到环境变量,需手动指定路径
        // System.setProperty("webdriver.chrome.driver", "你的ChromeDriver路径");
        
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        try {
            // 打开谷歌搜索页
            driver.get("https://www.google.com");

            // 等待搜索框加载并输入关键词
            WebElement searchInput = new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
            searchInput.sendKeys("selenium");
            searchInput.submit();

            // 等待第一页结果加载,定位结果条目
            List<WebElement> firstPageItems = new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.g, div.MjjYud")));
            // 统计非<a>标签的结果数量
            long firstPageCount = 0;
            for (WebElement item : firstPageItems) {
                if (!"a".equals(item.getTagName())) {
                    firstPageCount++;
                }
            }
            System.out.println("第一页非<a>标签结果数量:" + firstPageCount);

            // 定位并点击第二页按钮
            WebElement secondPageLink = new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@aria-label='第 2 页']")));
            secondPageLink.click();

            // 统计第二页非<a>标签结果数量
            List<WebElement> secondPageItems = new WebDriverWait(driver, Duration.ofSeconds(10))
                    .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.g, div.MjjYud")));
            long secondPageCount = 0;
            for (WebElement item : secondPageItems) {
                if (!"a".equals(item.getTagName())) {
                    secondPageCount++;
                }
            }
            System.out.println("第二页非<a>标签结果数量:" + secondPageCount);

            // 断言两页数量相等
            assertEquals("两页非<a>标签结果数量不一致", firstPageCount, secondPageCount);
            System.out.println("断言通过:两页非<a>标签结果数量相等");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }
}

注意事项

  • 确保ChromeDriver版本与本地Chrome浏览器版本完全匹配,避免兼容性问题
  • 若谷歌页面元素更新导致定位失败,需调整CSS选择器或XPath表达式
  • 显式等待的时长可根据网络状况适当调整
  • 运行代码前需确保已引入Selenium和JUnit的依赖(Maven或Gradle)

内容的提问来源于stack exchange,提问作者nasrallah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 04:16:22