zoukankan      html  css  js  c++  java
  • Webdriver中实现区域截图的方式以及如何截取frame中的图片

    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();
            }
        }

    }

  • 相关阅读:
    斯坦福【概率与统计】课程笔记(〇):前言
    斯坦福【概率与统计】课程笔记(二):从EDA开始
    dot
    左移右移
    树莓派系统烧写+网络配置
    windows上面捕获声卡数据
    python1
    继续进销存系统
    base64编码
    DES原理
  • 原文地址:https://www.cnblogs.com/zw520ly/p/5875546.html
Copyright © 2011-2022 走看看