Selenium 的 TakesScreenshot 接口默认返回一个临时文件,自己定义保存新路径非复制
方法 1:使用 OutputType.FILE + 移动文件(推荐)
虽然仍然涉及文件操作,但比复制更高效(直接重命名移动):
File tempScreenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String customPath = "C:/screenshots/my_custom_name.png"; // 你的自定义路径 File customFile = new File(customPath); tempScreenshot.renameTo(customFile); // 直接重命名移动 BufferedImage fullImg = ImageIO.read(customFile);
方法 2:使用 OutputType.BYTES + 直接写入
完全避免临时文件,直接保存到目标路径:
浙公网安备 33010602011771号