前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【后端】如何使 Chrome Driver 和 Chrome 浏览保持版本一致

【后端】如何使 Chrome Driver 和 Chrome 浏览保持版本一致

作者头像
框架师
发布2023-09-01 18:58:09
4090
发布2023-09-01 18:58:09
举报
文章被收录于专栏:墨白的Java基地墨白的Java基地

前言

近期,我开发了一款能够自动发布文章到微信公众号的程序。在该程序中,我使用了自动化框架 selenium,同时需要使用到驱动程序 chromedriver。然而,其中一个问题是随着 Chrome 浏览器的自动更新,chromedriver 驱动程序的版本可能无法与之保持一致。为了解决这个问题,我花了一些时间编写了下面的小程序,以确保 Chrome 浏览器和 chromedriver 驱动程序始终保持版本同步。

示例代码

代码语言:javascript
复制
package com.mobaijun.actual;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.http.HttpUtil;
import com.mobaijun.constant.Constant;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * software:IntelliJ IDEA 2023.1.1<br>
 * class name: UpdateChromeDriverActual<br>
 * class description: 自动更新 chromedriver 驱动和 Google 浏览器保持一致
 *
 * @author MoBaiJun 2023/7/16 20:50
 */
public class UpdateChromeDriverActual {

    /**
     * 根据目录获取 Google 浏览器版本
     *
     * @param directoryPath 目录地址
     * @return 浏览器版本号
     */
    public static String getInstalledChromeVersion(String directoryPath) {
        List<String> subdirectories = new ArrayList<>();
        try {
            Files.list(Path.of(directoryPath))
                    .filter(Files::isDirectory)
                    .forEach(path -> subdirectories.add(path.getFileName().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return subdirectories.get(0);
    }

    private static void downloadAndInstallChromeDriver(String chromeVersion) {
        try {
            // 构建 ChromeDriver 的版本 URL
            String latestReleaseUrl = "https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_" + chromeVersion.split("\\.")[0];
            // 获取最新版本号
            String latestVersion = HttpUtil.get(latestReleaseUrl).trim();
            // 构建 ChromeDriver 下载 URL
            String downloadUrl = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" + latestVersion + "/win64/chromedriver-win64.zip";

            // 设置下载的压缩文件路径
            String zipFilePath = Constant.RESOURCES_PATH + File.separator + "chromedriver.zip";
            // 设置解压后的目录路径
            String unzipDirectory = Constant.RESOURCES_PATH + File.separator + "chromedriver";

            // 使用 Hutool 下载文件
            HttpUtil.downloadFile(downloadUrl, zipFilePath);

            // 使用 Hutool 解压缩文件到目标目录
            ZipUtil.unzip(zipFilePath, unzipDirectory);

            // 删除下载的压缩文件
            FileUtil.del(zipFilePath);

            // 将解压后的 ChromeDriver 移动到 Google 目录
            FileUtil.move(new File(unzipDirectory), new File(Constant.CHROME_DRIVER_UPDATE_PATH), true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String chromeVersion = getInstalledChromeVersion(Constant.CHROME_VERSION);
        if (chromeVersion != null) {
            downloadAndInstallChromeDriver(chromeVersion);
            Console.log("Google Chrome driver updated successfully!");
        } else {
            Console.error("Unable to get Chrome browser version number. Make sure Chrome is installed and able to connect to the internet.");
        }
    }
}

需要注意的是,getInstalledChromeVersion 方法用于获取 Windows 系统上已安装的 Chrome 浏览器的最新版本号。该方法基于默认的安装路径,如果安装路径不同,需要相应地进行修改。downloadAndInstallChromeDriver 方法主要用于比对获取到的 Chrome 浏览器版本与最新的 chromedriver API 版本是否一致,如果不一致,则进行更新。随后,它会下载并将 chromedriver 安装到指定的路径中。在代码中,我使用了 Hutool 这个第三方库来进行文件下载和解压操作。经过多次测试,代码正常运行。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
    • 示例代码
    相关产品与服务
    云开发 CloudBase
    云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档