首页 > Java > java教程 > 正文

使用 Java Selenium 精准点击 Google 搜索结果与导航

心靈之曲
发布: 2025-11-06 14:47:01
原创
1006人浏览过

使用 java selenium 精准点击 google 搜索结果与导航

本教程详细介绍了如何使用 Java Selenium WebDriver 自动化 Google 搜索过程,包括处理 Cookie 同意弹窗、输入搜索关键词、提交搜索请求,以及从搜索结果中准确识别并点击目标链接。文章通过一个完整的代码示例,演示了如何利用不同的元素定位策略(如 ID、Name、Class Name、Tag Name)来有效与网页元素交互,并提供了关键步骤的解析与注意事项,旨在帮助开发者掌握自动化网页导航的核心技能。

引言

在使用 Java Selenium 进行网页自动化测试或数据抓取时,经常会遇到需要与动态网页元素交互的场景,例如在搜索引擎中输入关键词并点击搜索结果。本文将专注于解决一个常见问题:如何在使用 Java Selenium 成功执行 Google 搜索后,准确地点击搜索结果列表中的特定链接。我们将通过一个具体的案例,详细讲解实现这一目标的步骤和技巧。

环境准备

在开始之前,请确保您的开发环境中已配置好以下组件:

  1. Java Development Kit (JDK):版本 8 或更高。
  2. Maven 或 Gradle:用于项目管理和依赖引入。
  3. Selenium WebDriver 库:在 pom.xml (Maven) 或 build.gradle (Gradle) 中添加 Selenium 依赖。
  4. ChromeDriver:与您安装的 Chrome 浏览器版本兼容的 ChromeDriver 可执行文件。您可以从 ChromeDriver 官方网站 下载。为了简化驱动管理,强烈推荐使用 WebDriverManager 库,它能自动下载并配置所需的浏览器驱动。

Maven 依赖示例:

立即学习Java免费学习笔记(深入)”;

<dependencies>
    <!-- Selenium Java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.2</version> <!-- 请使用最新稳定版本 -->
    </dependency>
    <!-- WebDriverManager (可选,但强烈推荐) -->
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.3.0</version> <!-- 请使用最新稳定版本 -->
    </dependency>
</dependencies>
登录后复制

核心步骤解析

我们将通过以下步骤实现 Google 搜索结果的点击:

1. 初始化 WebDriver

首先,需要初始化 WebDriver 实例,这里我们使用 ChromeDriver。如果使用 WebDriverManager,则无需手动设置 webdriver.chrome.driver 系统属性。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager; // 如果使用 WebDriverManager

// ...
public static void main(String[] args) {
    // 使用 WebDriverManager 自动设置 ChromeDriver
    WebDriverManager.chromedriver().setup();
    // 或者手动设置 ChromeDriver 路径
    // System.setProperty("webdriver.chrome.driver", "path/to/your/chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize(); // 最大化浏览器窗口以确保元素可见
    // ...
}
登录后复制

2. 导航至 Google 并处理 Cookie 同意弹窗

许多网站(包括 Google)在首次访问时会显示 Cookie 同意弹窗。在进行后续操作前,必须处理这些弹窗。

driver.get("https://www.google.cz"); // 导航到 Google 网站

// 尝试接受 Cookie 弹窗。注意:Google 的 Cookie 弹窗元素 ID 或结构可能随时间变化。
// L2AGLb 是一个常见的同意按钮 ID
List<WebElement> acceptBtns = driver.findElements(By.id("L2AGLb"));
if (!acceptBtns.isEmpty() && acceptBtns.get(0).isDisplayed()) {
    acceptBtns.get(0).click();
} else {
    // 如果没有找到特定 ID 的按钮,可以尝试查找包含 "Accept all" 或 "我同意" 文本的按钮
    List<WebElement> potentialAcceptButtons = driver.findElements(By.xpath("//button[contains(.,'Accept all') or contains(.,'我同意')]"));
    if (!potentialAcceptButtons.isEmpty() && potentialAcceptButtons.get(0).isDisplayed()) {
        potentialAcceptButtons.get(0).click();
    }
}
登录后复制

注意事项: Cookie 同意弹窗的元素定位器(ID、XPath 等)可能会随着网站更新而改变。在实际应用中,您可能需要根据最新的网页结构进行调整。

3. 输入搜索关键词

定位到 Google 搜索框,并使用 sendKeys() 方法输入搜索关键词。

WebElement searchInput = driver.findElement(By.name("q")); // 'q' 是 Google 搜索框的 name 属性
searchInput.sendKeys(mySearchString); // mySearchString 是您要搜索的字符串
登录后复制

4. 提交搜索请求

Google 搜索页面通常有多个同名的提交按钮。我们需要找到并点击可见的那个。

List<WebElement> searchBtns = driver.findElements(By.name("btnK")); // 'btnK' 是 Google 搜索按钮的 name 属性
for (WebElement searchBtn : searchBtns) {
    // 通过判断元素位置和可见性来确定正确的搜索按钮
    Point p = searchBtn.getLocation();
    if (p.getX() > 0 && p.getY() > 0 && searchBtn.isDisplayed()) {
        searchBtn.click();
        break;
    }
}
登录后复制

这里使用 getLocation() 检查元素在页面上的坐标,结合 isDisplayed() 确保元素可见,从而排除隐藏的或不在视口内的同名按钮。

5. 定位并点击搜索结果

搜索结果通常包含在一个特定的 HTML 结构中。我们需要识别这些结构,然后从中提取出实际的链接元素。

纳米搜索
纳米搜索

纳米搜索:360推出的新一代AI搜索引擎

纳米搜索 30
查看详情 纳米搜索
List<WebElement> resultLinks = new ArrayList<>();
// 定位所有包含搜索结果的 div 元素
// 'yuRUbf' 是 Google 搜索结果条目常用的 class name
List<WebElement> searchResultDivs = driver.findElements(By.className("yuRUbf"));

if (!searchResultDivs.isEmpty()) {
    for (WebElement searchResultDiv : searchResultDivs) {
        // 在每个结果 div 中找到第一个 <a> 标签,即实际的链接
        resultLinks.add(searchResultDiv.findElement(By.tagName("a")));
    }

    // 点击第一个搜索结果链接
    if (!resultLinks.isEmpty()) {
        resultLinks.get(0).click(); // 点击列表中的第一个链接

        // 页面已切换,打印当前 URL 和标题进行验证
        System.out.println("Current URL: " + driver.getCurrentUrl());
        System.out.println("Current title: " + driver.getTitle());
    } else {
        System.out.println("未找到任何搜索结果链接。");
    }
} else {
    System.out.println("未找到任何搜索结果容器。");
}
登录后复制

这里利用 By.className("yuRUbf") 找到每个搜索结果的容器,然后使用 findElement(By.tagName("a")) 在该容器内部进一步定位到实际的超链接。这种组合定位方式比直接使用复杂的 XPath 更具鲁棒性。

6. 关闭浏览器

完成所有操作后,务必关闭浏览器实例以释放资源。

driver.quit();
登录后复制

完整示例代码

下面是整合上述所有步骤的完整 Java Selenium 代码示例:

package com.example.selenium;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit; // 用于隐式等待,可选

public class GoogleSearchClickTutorial {

    public static void main(String[] args) {
        // 1. 初始化 WebDriver
        WebDriverManager.chromedriver().setup(); // 自动下载并配置 ChromeDriver
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize(); // 最大化浏览器窗口
        // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // 设置隐式等待,可选

        List<WebElement> resultLinks = new ArrayList<>();
        String mySearchString = "fantomas wiki"; // 定义搜索关键词

        try {
            // 2. 导航至 Google 并处理 Cookie 同意弹窗
            driver.get("https://www.google.cz");

            // 尝试接受 Cookie 弹窗
            // 注意:Google 的 Cookie 弹窗元素 ID 或结构可能随时间变化
            List<WebElement> acceptBtns = driver.findElements(By.id("L2AGLb"));
            if (!acceptBtns.isEmpty() && acceptBtns.get(0).isDisplayed()) {
                System.out.println("找到并点击了 Cookie 同意按钮。");
                acceptBtns.get(0).click();
            } else {
                // 备用方案:查找包含特定文本的按钮
                List<WebElement> potentialAcceptButtons = driver.findElements(By.xpath("//button[contains(.,'Accept all') or contains(.,'我同意') or contains(.,'全部接受')]"));
                if (!potentialAcceptButtons.isEmpty() && potentialAcceptButtons.get(0).isDisplayed()) {
                    System.out.println("找到并点击了备用 Cookie 同意按钮。");
                    potentialAcceptButtons.get(0).click();
                } else {
                    System.out.println("未找到 Cookie 同意按钮或其不可见,尝试继续。");
                }
            }

            // 3. 输入搜索关键词
            WebElement searchInput = driver.findElement(By.name("q"));
            searchInput.sendKeys(mySearchString);
            System.out.println("已输入搜索关键词: " + mySearchString);

            // 4. 提交搜索请求
            List<WebElement> searchBtns = driver.findElements(By.name("btnK"));
            boolean clickedSearchButton = false;
            for (WebElement searchBtn : searchBtns) {
                Point p = searchBtn.getLocation();
                if (p.getX() > 0 && p.getY() > 0 && searchBtn.isDisplayed()) {
                    searchBtn.click();
                    clickedSearchButton = true;
                    System.out.println("已点击搜索按钮。");
                    break;
                }
            }
            if (!clickedSearchButton) {
                System.out.println("未找到可见的搜索按钮。");
            }

            // 5. 定位并点击搜索结果
            // 等待搜索结果加载,这里可以考虑使用显式等待
            // new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.className("yuRUbf")));

            List<WebElement> searchResultDivs = driver.findElements(By.className("yuRUbf"));
            if (!searchResultDivs.isEmpty()) {
                System.out.println("找到 " + searchResultDivs.size() + " 个搜索结果容器。");
                for (WebElement searchResultDiv : searchResultDivs) {
                    // 获取 div 中的第一个 <a> 标签
                    resultLinks.add(searchResultDiv.findElement(By.tagName("a")));
                }

                // 点击第一个搜索结果链接
                if (!resultLinks.isEmpty()) {
                    resultLinks.get(0).click();
                    System.out.println("已点击第一个搜索结果链接。");

                    // 页面已切换,打印当前 URL 和标题进行验证
                    System.out.println("当前 URL: " + driver.getCurrentUrl());
                    System.out.println("当前标题: " + driver.getTitle());
                } else {
                    System.out.println("未找到任何搜索结果链接。");
                }
            } else {
                System.out.println("未找到任何搜索结果容器。");
            }

        } catch (Exception e) {
            System.err.println("发生错误: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 6. 关闭浏览器
            if (driver != null) {
                driver.quit();
                System.out.println("浏览器已关闭。");
            }
        }
    }
}
登录后复制

示例输出:

Starting ChromeDriver 107.0.5304.62 (1eec40d3a5764881c92085aaee66d25075c159aa-refs/branch-heads/5304@{#942}) on port 20110
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Lis 29, 2022 11:10:07 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
找到并点击了 Cookie 同意按钮。
已输入搜索关键词: fantomas wiki
已点击搜索按钮。
找到 10 个搜索结果容器。
已点击第一个搜索结果链接。
当前 URL: https://cs.wikipedia.org/wiki/Fantomas
当前标题: Fantomas – Wikipedie
浏览器已关闭。
登录后复制

(注:实际输出中的日期和 ChromeDriver 版本信息可能因您的环境和时间而异。)

注意事项与总结

  1. 元素定位策略选择: 优先使用 ID、Name、Class Name 进行定位,它们通常比 XPath 更稳定高效。当这些定位器不可用时,再考虑使用 CSS Selector 或 XPath。避免使用过于复杂的 XPath,因为它们对页面结构变化非常敏感。

  2. 动态元素和等待机制: 网页元素加载是异步的,尤其在处理弹窗或 AJAX 请求时。本示例中虽然没有显式使用等待,但在实际项目中,强烈建议使用 WebDriverWait 结合 ExpectedConditions 来等待元素出现或可点击,以提高脚本的稳定性。例如:

    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import java.time.Duration; // Java 8+
    
    // ...
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("someId")));
    element.click();
    登录后复制
  3. 页面结构变化: Google 等大型网站的 UI 结构可能会频繁更新。这意味着您编写的定位器(如 By.id("L2AGLb") 或 By.className("yuRUbf"))可能在未来某个时间点失效。定期检查并更新定位器是维护自动化脚本的必要工作。

  4. 错误处理: 在自动化脚本中加入 try-catch-finally 块是一个好习惯,可以捕获潜在的 NoSuchElementException 或其他运行时错误,并确保在脚本执行完毕(无论成功与否)都能正确关闭浏览器。

  5. 浏览器最大化: driver.manage().window().maximize() 有助于确保所有元素都在可视区域内,减少因元素不在视口内而导致的点击失败问题。

通过本文的详细教程和示例代码,您应该已经掌握了使用 Java Selenium 自动化 Google 搜索并点击搜索结果的关键技术。理解并灵活运用各种元素定位策略,结合适当的等待机制和错误处理,将使您的自动化脚本更加健壮和可靠。

以上就是使用 Java Selenium 精准点击 Google 搜索结果与导航的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号