Selenium Webdriver实现截图功能的示例

2025-05-29 0 41

前几天在研究中自动化的时候突发奇想,想着能不能来截个图,以便之后查看,实现的方法其实也不难,毕竟selenium webdriver已经提供了截图额功能,TakesScreenshot接口函数(英文意思就是获取屏幕截图takes-screenshot)。

废话不多说了,直接上代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
package com.wch;

import java.io.File;

import java.io.IOException;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.WebDriverWait;

import com.sun.jna.platform.FileUtils;

public class TestTakesScreenshot {

public static void main(String[] args) {

System.setProperty("webdriver.firefox.bin", "D:\\\\Program Files (x86)\\\\Mozilla Firefox\\\\firefox.exe");

WebDriver driver = new FirefoxDriver();

driver.get("http://www.baidu.com");

File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //讲截取的图片以文件的形式返回

try {

org.apache.commons.io.FileUtils.copyFile(srcFile, new File("d:\\\\screenshot.png")); //使用copyFile()方法保存获取到的截图文件

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

driver.quit();

}

}

还有其他方法的话希望各位也能提供下,互相学习。

以上这篇Selenium Webdriver实现截图功能的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/woiangaoiowe/article/details/60963291

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Selenium Webdriver实现截图功能的示例 https://www.kuaiidc.com/111999.html

相关文章

发表评论
暂无评论