当前位置: 首页 > news >正文

快速使用selenium+java案例

maven配置Antlr4插件案例Demo

前置ChromeDriver准备

ChromeDriver地址使用

ChromeDriver官方地址:https://googlechromelabs.github.io/chrome-for-testing/

示范官方下载地址:

https://storage.googleapis.com/chrome-for-testing-public/139.0.7258.66/linux64/chrome-linux64.zip

但是你若是直接用java api去请求可能会报连接不上的问题,我们这里选择使用国内镜像:

# 淘宝
https://registry.npmmirror.com/binary.html?path=chromedriver/# 华为
https://mirrors.huaweicloud.com/chromedriver/# 腾讯(无chrome driver)
https://mirrors.cloud.tencent.com/chromedriver/

我们这里使用华为云的:

https://mirrors.huaweicloud.com/chromedriver/138.0.7204.183/chromedriver-mac-arm64.zip

快速下载ChromeDriver & 设置可执行权限 & 环境变量

引入pom.xml依赖

这里我们引入hutool & selenium相关的依赖包:

<dependencies><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.35</version></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.25.0</version></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-chrome-driver</artifactId><version>4.25.0</version></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-api</artifactId><version>4.25.0</version></dependency><!--  支持 触摸屏操作 的接口      --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-remote-driver</artifactId><version>4.25.0</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.21</version> <!-- 或更高版本 --></dependency>
</dependencies>

工具类封装(HutoolUtil、ChromeDriverUtil)

image-20250809013453564

HutoolUtil.java(封装下载 & 解压功能)
package com.changlu.autosyncblog.util;import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.extra.compress.CompressUtil;
import cn.hutool.extra.compress.extractor.Extractor;
import cn.hutool.http.HttpRequest;/*** Hutool工具类,提供文件下载和解压缩功能* 该类使用Hutool库实现文件下载和解压缩操作。*/
public class HutoolUtil {/*** 下载文件到指定路径* @param fileUrl 文件的URL地址* @param downloadPath 下载后的文件保存路径*/public static void download(String fileUrl, String downloadPath) {HttpRequest.get(fileUrl).setFollowRedirects(true).header("User-Agent", "Mozilla/5.0").timeout(30_000).execute().writeBody(FileUtil.file(downloadPath));}/*** 解压缩文件到指定路径* @param downloadPath 下载的压缩文件路径* @param extractPath 解压后的文件保存路径*/public static void extract(String downloadPath, String extractPath) {Extractor extractor = CompressUtil.createExtractor(CharsetUtil.defaultCharset(),FileUtil.file(downloadPath));extractor.extract(FileUtil.file(extractPath));}}
http://www.xdnf.cn/news/1265977.html

相关文章:

  • [Linux]学习笔记系列 -- [arm][lds]
  • 2022 RoboCom 世界机器人开发者大赛-本科组(国赛)
  • 前端工程化:从构建工具到性能监控的全流程实践
  • 2G内存的服务器用宝塔安装php的fileinfo拓展时总是卡死无法安装成功的解决办法
  • Ubuntu下搭建LVGL模拟器
  • 【第2.1话:基础知识】基于Ubuntu的ROS环境搭建与车辆可视化编程实践:初学者指南及RVIZ应用(含作业及代码)
  • Ubuntu Server 22 虚拟机空间扩容
  • ubuntu dpkg命令使用指南
  • 从零玩转Linux云主机:免费申请、连接终端、命令速查表
  • 【SQL进阶】用EXPLAIN看透SQL执行计划:从“盲写“到“精准优化“
  • 【JavaEE】(11) 前端基础三件套
  • 比亚迪第五代DM技术:AI能耗管理的深度解析与实测验证
  • 数学与应用数学:到底有啥区别?
  • Kafka学习记录
  • 建筑物实例分割数据集-9,700 张图片 城市规划与发展 灾害评估与应急响应 房地产市场分析 智慧城市管理 地理信息系统(GIS) 环境影响评估
  • Java安全-组件安全
  • 关于灰度图像相似度的损失函数(笔记)
  • C++安全异常设计
  • 华为交换机进阶功能和场景化配置
  • Javaweb - 14.1 - 前端工程化
  • day16 - CSS3新增属性
  • 利用whisper api实现若无字幕则自动下载音频并用 whisper 转写,再用 LLM 总结。
  • Blender 快捷键速查表 (Cheat Sheet)
  • 数据结构(9)——排序
  • 202506 电子学会青少年等级考试机器人二级理论综合真题
  • 本文章分享一个本地录音和实时传输录音给app的功能(杰理)
  • java10学习笔记
  • IntelliJ IDEA 新手全方位使用指南
  • 服务机器人选择屏幕的逻辑
  • 7、docker |其余命令