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

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM