[英]Add images to cells of an excel sheet using selenium
我正在嘗試從網頁中捕獲元素的屏幕截圖,並希望將捕獲的屏幕截圖寫入Excel工作表的單元格。
請在下面的代碼中找到。 我無法決定要為Label的第三個參數傳遞什么
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();
}
}
您應該使用以下方法:
// **Label lab = new Label(0, 0, );**
// wsheet.addCell(lab);
WritableImage image = new WritableImage(0, 0, 4, 6, screenshotLocation);
wsheet.addImage(image);
// Parameters:
// x - the column number at which to position the image
// y - the row number at which to position the image
// width - the number of columns cells which the image spans
// height - the number of rows which the image spans
// image - the source image file
您應將這兩個參數“ 4、6”替換為實際大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.