在硒中使用safari驱动程序的图像位置

时间:2015-06-14 17:44:24

标签: java selenium

我尝试制作网页的截屏,然后检索图像的大小和位置,最后将图像保存在文件中。

摘录:

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之间是否存在差异?

1 个答案:

答案 0 :(得分: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);

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