如何使用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);

}

}

以上是 如何使用Selenium WebDriver拍摄部分屏幕截图(框架)? 的全部内容, 来源链接: utcz.com/qa/402574.html

回到顶部