您的位置:首页 > 其它

Webdriver中实现区域截图的方式以及如何截取frame中的图片

2016-09-15 22:12 567 查看
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ScreenShot {
public void screenShotForElement(WebDriver driver,WebElement element, String path,int x,int y) throws InterruptedException {
//截取整个页面的图片
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
//获取元素在所处frame中位置对象
Point p = element.getLocation();
//获取元素的宽与高
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
//矩形图像对象
Rectangle rect = new Rectangle(width, height);
BufferedImage img = ImageIO.read(scrFile);
//x、y表示加上当前frame的左边距,上边距
BufferedImage dest = img.getSubimage(p.getX()+x, p.getY()+y,rect.width, rect.height);
ImageIO.write(dest, "png", scrFile);
FileUtils.copyFile(scrFile, new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐