如何使用selenium java拍摄彩色屏幕截图
截图被截图,如下图所示,但我想用实际颜色截图,我也不知道背后的原因,有人能告诉我为什么会发生这种情况吗
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,131)");
int yPosition = Decision_Maker.getLocation().getY();
for(int j = 1; j<=list.size(); j++)
{
// Get entire page screenshot
File screenshots = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshots);
ImageIO.read(screenshots).getHeight();
// Get width and height of the element
int eleWidth = Decision_Maker.getSize().getWidth();
int eleHeight = Decision_Maker.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(465, 190,eleWidth,eleHeight);
ImageIO.write(eleScreenshot, "jpg", screenshots);
//Scroll vertically to the element
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scroll (0, " + yPosition + ") ");
yPosition = yPosition + eleHeight;
//wait for sometime
Thread.sleep(3000);
//File Location
String location = "E:\\Automation\\Screenshots\\";
Thread.sleep(3000);
//capture screenshot
FileUtils.copyFile(screenshots, new File (location + "img" + i + ".jpg"));
Thread.sleep(3000L);
}
# 1 楼答案
您可以创建utils类来截图并使用它