java操作selenium浏览器自动化操作
2022/8/26 14:24:54
本文主要是介绍java操作selenium浏览器自动化操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
selenium github
selenium官网
各类型浏览器webDriver驱动下载
chrome浏览器webDriver驱动下载,注意要与电脑上实际安装的浏览器版本相对应
原理说明:
java代码直接通过selenium-java库中的核心类ChromeDriver调用本地下载的webDriver驱动,webDriver驱动会自动找到其对应操作系统安装的浏览器模仿人类执行相关的操作
POM依赖
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency>
selenium-java依赖版本过高会导致下方异常
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/internal/Require
....
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.internal.Require
参考:https://blog.csdn.net/m0_56758840/article/details/123380448
import cn.hutool.core.img.ImgUtil; import cn.hutool.core.io.FileUtil; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.awt.image.BufferedImage; /** * @author JHL * @version 1.0 * @date 2022/8/25 14:50 * @since : JDK 11 */ public class T { public static void main(String[] args) throws InterruptedException { // 设置jvm系统属性,selenium会读取这个数据找到要使用的webDriver驱动 System.setProperty("webdriver.chrome.driver", "D:/env/chromedriver_win32/chromedriver.exe"); // 设置驱动的选项 ChromeOptions options = new ChromeOptions(); // 设置无头浏览器,即不打开窗口渲染,都在内存中执行 options.setHeadless(true); // 根据选项实例化驱动 ChromeDriver driver = new ChromeDriver(options); // 访问百度,并搜索hello world driver.get("http://www.baidu.com"); driver.findElement(By.name("wd")).sendKeys("hello word!"); driver.findElement(By.id("su")).click(); Thread.sleep(5000); // 搜索结果页面的html源码 System.out.println(driver.getPageSource()); // 截图方式一 // File file = driver.getScreenshotAs(OutputType.FILE); // 截图方式二 BufferedImage screenshot = driver.getScreenshotAs(new OutputType<>() { @Override public BufferedImage convertFromBase64Png(String base64) { return ImgUtil.toImage(base64); } @Override public BufferedImage convertFromPngBytes(byte[] bytes) { return ImgUtil.toImage(bytes); } }); ImgUtil.write(screenshot, FileUtil.newFile("截图.png")); driver.quit(); }
使用参考:
https://blog.csdn.net/chenjxj123/article/details/121802904
https://www.cnblogs.com/ychun/p/14282422.html
这篇关于java操作selenium浏览器自动化操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行
- 2024-05-08阿里云域名注册流程,分享给第一次购买域名的新手站长!
- 2024-05-082024年,行业变动下的程序员应该首先学习哪种编程语言?