gpt4 book ai didi

selenium - 如何在Ashot selenium中将屏幕截图对象转换为文件对象

转载 作者:行者123 更新时间:2023-12-02 08:55:05 27 4
gpt4 key购买 nike

我写了一个java方法,其返回类型是file。此方法使用 Ashot 抓取屏幕截图并将其存储在 Screenshot 对象中。我需要将该屏幕截图对象转换为文件对象,以便我可以返回文件对象。

   public static File grabScreenshot() {

try {

Thread.sleep(Integer.parseInt(Property.getProperty("screenshotDelay")));

} catch (InterruptedException e) {
e.printStackTrace();

} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

File screenshot=null; //creating null file object to return

Screenshot screenshot1 = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(driver());

//Here I have to typecast the screenshot1 to file type so that I can return
return screenshot;
}

最佳答案

这应该可以解决问题

// getImage() will give buffered image which can be used to write to file
BufferedImage bi = new AShot()
.shootingStrategy(ShootingStrategies.viewportPasting(100))
.takeScreenshot(driver).getImage();

File outputfile = new File("image.jpg");
try {
ImageIO.write(bi, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}

// Print the absolute path to see where the file is created
System.out.println(outputfile.getAbsolutePath());

关于selenium - 如何在Ashot selenium中将屏幕截图对象转换为文件对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692095/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com