加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Selenium Webdriver实现截图功能的示例

发布时间:2020-12-14 19:44:56 所属栏目:Java 来源:网络整理
导读:前几天在研究中自动化的时候突发奇想,想着能不能来截个图,以便之后查看,实现的方法其实也不难,毕竟selenium webdriver已经提供了截图额功能,TakesScreenshot接口函数(英文意思就是获取屏幕截图takes-screenshot)。 废话不多说了,直接上代码 package

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

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

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 Firefoxfirefox.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实现截图功能的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • Python中使用 Selenium 实现网页截图实例
  • Python实现屏幕截图的两种方式
  • Python 实现网页自动截图的示例讲解
  • Python实现屏幕截图的代码及函数详解

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读