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

    }

  • 相关阅读:
    HDU 5671 矩阵
    HDU 5670
    UVA 11995 STL 使用
    VK Cup 2016
    字段定义
    apache用户
    apache
    使用第三方登录
    setex()
    如果客户端禁用了cookie,如何实现session
  • 原文地址:https://www.cnblogs.com/zw520ly/p/5875546.html
Copyright © 2011-2022 走看看