如何使用Selenium WebDriver进行部分屏幕截图(框架)?

是否可以在框架集中仅使用一个框架(而不是整个窗口)的WebDriver截图?

或者,是否可以定义屏幕的窗口的坐标,然后裁剪图像?

这应该工作:

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class Shooter{

    private WebDriver driver;

    public void shootWebElement(WebElement element) throws IOException  {

        File screen = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);

        Point p = element.getLocation();

        int width = element.getSize().getWidth();
        int height = element.getSize().getHeight();

        BufferedImage img = ImageIO.read(screen);

        BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width,   
                                 height);

        ImageIO.write(dest, "png", screen);

        File f = new File("S:\\ome\\where\\over\\the\\rainbow");

        FileUtils.copyFile(screen, f);

    }
}
http://stackoverflow.com/questions/10848900/how-to-take-partial-screenshot-frame-with-selenium-webdriver

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:如何使用Selenium WebDriver进行部分屏幕截图(框架)?