惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
H
Help Net Security
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
博客园 - 叶小钗
Y
Y Combinator Blog
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
C
Check Point Blog
Recent Announcements
Recent Announcements
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
B
Blog

博客园 - 狗狗听话

SpringBoot学习之缓存@Caching, @Cacheable,@CacheEvict Java学习之 EasyExcel Java学习之LocalDateTime, LocalDate, LocalTime python在某个目录下的查找指定文件夹 SpringBoot学习之JPA设置多个主键的方法 Python使用psycopg2的copy_from方法高效的大量数据批量导入 SpringBoot学习之文件上传(利用postman测试) SpringBoot学习之文件上传,下载 Python常用的语法糖 Python学习之datetime Java学习之 stream 常用方法 SpringBoot学习之使用idea打包jar文件 React学习之数组 Java学习之日期时间转换 Java读取execl 利用JDBC插入Postgresql简单示例 Springboot中项目启动加载的几种方式 Macbook pro 安装Redis Macbook pro 打开pgAmin报错 SpringBoot学习之任务定时器 关于git rebase中的一些问题:Git报错“no branch, rebasing master”
Java selenium实现浏览器ctrl + s功能
狗狗听话 · 2025-03-31 · via 博客园 - 狗狗听话

1.chromedirver驱动下载:https://link.zhihu.com/?target=https%3A//storage.googleapis.com/chrome-for-testing-public/137.0.7151.104/mac-arm64/chromedriver-mac-arm64.zip

红色字体部分换成chrome浏览器的版本即可。

备用地址2:https://googlechromelabs.github.io/chrome-for-testing/

2.pom.xml文件

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.27.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.27.0</version>
</dependency>

3.代码示例

public void load(String url, String pageName) throws IOException {
// 设置 EdgeOptions (或 ChromeOptions)
ChromeOptions options = new ChromeOptions(); //或 ChromeOptions
// 初始化 EdgeDriver (或 ChromeDriver)
System.setProperty("webdriver.chrome.driver", "你的chromedriver文件路径");
WebDriver driver = new ChromeDriver(options); //或 WebDriver driver = new ChromeDriver(options);
try {
// 访问网页
driver.get(url);
// 等待网页加载完成 (可以根据实际情况调整等待时间)
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); //Java 8+
wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")));
String html = driver.getPageSource();
FileWriter fileWriter = new FileWriter(pageName);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.write(html);
writer.flush();
writer.close();
Thread.sleep(3000);
} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
e.printStackTrace();
} finally {
driver.quit(); //关闭浏览器
}
}