这种分离是故意识地努力让浏览器供应商为其浏览器的实现卖力的一部分。 Selenium 在可能的情形下利用这些第三方驱动程序, 但是在这些驱动程序不存在的情形下,它也供应了由项目自己掩护的驱动程序。
Selenium 框架通过一个面向用户的界面将所有这些部分连接在一起, 该界面许可透明地利用不同的浏览器后端, 从而实现跨浏览器和跨平台自动化。
# selenium 驱动https://selenium-python.readthedocs.io/installation.html#drivershttps://selenium-python.readthedocs.io/api.html
selenium-java紧张依赖
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.16.1</version> </dependency>
测试代码
// 获取 java 版本 String version = System.getProperty("java.specification.version"); // 获取系统类型 String platform = System.getProperty("os.name", ""); platform = platform.toLowerCase().contains("window") ? "win" : "linux"; // 当出路序目录 String current = System.getProperty("user.dir"); System.out.println("current:" + current); // firefox 运行参数配置 FirefoxOptions options = new FirefoxOptions(); // 无头模式 options.addArguments("--headless"); // 最大化 options.addArguments("--start-maximized"); FirefoxDriver browser = new FirefoxDriver(options); Path url = Paths.get(current, "..", "index.html"); System.out.println("url:" + url.toString()); // NOTE 要利用 file 协议 browser.get(String.format("file://%s", url.toString())); // 打印设置 PrintOptions print = new PrintOptions(); Pdf pdf = browser.print(print); // pdf base64 内容 String content = pdf.getContent(); // 解码内容 Base64.Decoder decoder = Base64.getDecoder(); byte[] buffer = decoder.decode(content); try { // 将 byte 写入文件 Path file = Paths.get(String.format("java%s_%s.pdf", version, platform)); Files.write(file, buffer); } catch (IOException e) { throw new RuntimeException(e); } finally { browser.quit(); }
效果预览
selenium/java11_linux.pdf · yjihrp/linux-html2pdf-demo - Gitee.com

selenium/java11_win.pdf · yjihrp/linux-html2pdf-demo - Gitee.com
测试结果测试结果
下一篇 6-LINUX HTML 转 PDF-selenium-python