2015-06-14 79 views
1

我正在尝试制作网页的屏幕截图,然后检索图像的大小和位置并最终将图像保存到文件中。在硒中使用Safari浏览器驱动程序的图像位置

段:

this.driver.navigate().to(xxx); 

final byte[] arrScreen = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.BYTES); 
final BufferedImage imageScreen = ImageIO.read(new ByteArrayInputStream(arrScreen)); 
final WebElement cap = this.driver.findElement(By.id("myImageId")); 

final Dimension capDimension = cap.getSize(); 
final Point capLocation = cap.getLocation(); 
final BufferedImage imgCap = imageScreen.getSubimage(capLocation.x, capLocation.y, 
      capDimension.width, capDimension.height); 

final File file = new File(".../capture.png"); 
final FileOutputStream os = new FileOutputStream(file); 
ImageIO.write(imgCap, "png", os); 

使用Windows操作系统(ChromeDriver),但它不能正常工作使用Mac OS(ChromeDriver和SafariDriver都失败)(坐标是错误的)这项工作的罚款。

任何想法,为什么?

编辑:

是否有在Windows和Mac OS TakesScreenshot之间有什么区别?

回答

0

我换成这样的:

final BufferedImage imgCap = imageScreen.getSubimage(capLocation.x, capLocation.y, 
     capDimension.width, capDimension.height); 

这个和它工作得很好:

final BufferedImage imgCap = imageScreen.getSubimage(capLocation.x * 2, capLocation.y * 2, 
     capDimension.width * 2, capDimension.height * 2); 

但我仍然不知道为什么,任何想法?

相关问题