How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2593
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

30条回答
  •  渐次进展
    2020-11-21 08:16

    Java

    Seems to be missing here - taking screenshot of a specific element in Java:

    public void takeScreenshotElement(WebElement element) throws IOException {
        WrapsDriver wrapsDriver = (WrapsDriver) element;
        File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
        Rectangle rectangle = new Rectangle(element.getSize().width, element.getSize().height);
        Point location = element.getLocation();
        BufferedImage bufferedImage = ImageIO.read(screenshot);
        BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
        ImageIO.write(destImage, "png", screenshot);
        File file = new File("//path//to");
        FileUtils.copyFile(screenshot, file);
    }
    

提交回复
热议问题