如何使用webDriver(java)截图并将其逐个粘贴到word文件中

如何使用webDriver(java)截图并将其逐个粘贴到word文件中,java,selenium,webdriver,Java,Selenium,Webdriver,我能够通过((TakesScreenshot)driver.getScreenshotAs(OutputType.FILE)截图 在我的应用程序中,我必须为每个页面截图,所以我想将多个截图逐个保存到一个.doc文件中。 有API吗 有什么想法吗? 请帮助…Selenium Webdriver不提供在word文件中添加快照的任何功能。 为此,您需要使用第三方库 请参阅下文:- 您还可以使用reporter在TestNG输出文件中添加图像文件 请参阅下文:- 希望它能帮助你:)最简单的方法-截

我能够通过((TakesScreenshot)driver.getScreenshotAs(OutputType.FILE)截图

在我的应用程序中,我必须为每个页面截图,所以我想将多个截图逐个保存到一个.doc文件中。 有API吗

有什么想法吗?
请帮助…

Selenium Webdriver不提供在word文件中添加快照的任何功能。 为此,您需要使用第三方库

请参阅下文:-

您还可以使用reporter在TestNG输出文件中添加图像文件

请参阅下文:-


希望它能帮助你:)

最简单的方法-截图,放入PNG/JPEG文件,阅读,在MS Word中添加,删除文件,简单。这里有一个现成的代码供您使用。。。。宾果

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class TakeScreenshots {

    public static void main(String[] args) {
        try {

            XWPFDocument docx = new XWPFDocument();
            XWPFRun run = docx.createParagraph().createRun();
            FileOutputStream out = new FileOutputStream("d:/xyz/doc1.docx");

            for (int counter = 1; counter <= 5; counter++) {
                captureScreenShot(docx, run, out);
                TimeUnit.SECONDS.sleep(1);
            }

            docx.write(out);
            out.flush();
            out.close();
            docx.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void captureScreenShot(XWPFDocument docx, XWPFRun run, FileOutputStream out) throws Exception {

        String screenshot_name = System.currentTimeMillis() + ".png";
        BufferedImage image = new Robot()
                .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        File file = new File("d:/xyz/" + screenshot_name);
        ImageIO.write(image, "png", file);
        InputStream pic = new FileInputStream("d:/xyz/" + screenshot_name);
        run.addBreak();
        run.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, screenshot_name, Units.toEMU(350), Units.toEMU(350));
        pic.close();
        file.delete();
    }

}
导入java.awt.Rectangle;
导入java.awt.Robot;
导入java.awt.Toolkit;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.InputStream;
导入java.util.concurrent.TimeUnit;
导入javax.imageio.imageio;
导入org.apache.poi.util.Units;
导入org.apache.poi.xwpf.usermodel.XWPFDocument;
导入org.apache.poi.xwpf.usermodel.XWPFRun;
公共课截图{
公共静态void main(字符串[]args){
试一试{
XWPFDocument docx=新的XWPFDocument();
XWPFRun=docx.create段落().createRun();
FileOutputStream out=新的FileOutputStream(“d:/xyz/doc1.docx”);

对于(int counter=1;counter为什么要在Word文档中收集屏幕截图?在Word文档中收集屏幕截图有什么好处?以后是否应该用文本对其进行编辑或充实?为什么不使用pdf?此外,.doc是一种非常过时的格式。此外,简单地要求API是离题的。我们在这里帮助进行特定的pr编程问题,而不是搜索谷歌为你。