2016-09-20 110 views
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(); 

} 

}

+0

检查链接: http://stackoverflow.com/questions/28238078/insert-image-in-column-to-excel-using-apache-poi 想想,这可能会帮助你。 –

回答

1

你应该使用下面的方法:

// **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“。

相关问题