0
我試圖捕獲網頁元素的屏幕截圖,並且想要將捕獲的屏幕截圖寫入Excel表格的單元格。使用硒將圖像添加到Excel表單的單元格
請在下面找到代碼。我無法決定如何傳遞的第三個參數的標籤
WebDriver driver;
String baseUrl = "http://www.google.com";
@Test
public void test() throws IOException, BiffException, RowsExceededException, WriteException {
WebElement ele = driver.findElement(By.id("hplogo"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("D:\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
String excelPath = "D:\\" + appName + ".xls";
File file = new File(excelPath);
WritableWorkbook wbook = Workbook.createWorkbook(file);
WritableSheet wsheet = wbook.createSheet("Seeds", 0);
**Label lab = new Label(0, 0,);**
wsheet.addCell(lab);
wbook.write();
wbook.close();
}
}
檢查鏈接: http://stackoverflow.com/questions/28238078/insert-image-in-column-to-excel-using-apache-poi 想想,這可能會幫助你。 –