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 + 直接写入

完全避免临时文件,直接保存到目标路径:

byte[] screenshotBytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
String customPath = "C:/screenshots/my_custom_name.png";
try (FileOutputStream fos = new FileOutputStream(customPath)) {
    fos.write(screenshotBytes);
}
BufferedImage fullImg = ImageIO.read(new File(customPath));

 

 

posted @ 2025-06-16 06:35  锐洋智能  阅读(27)  评论(0)    收藏  举报