方式1:
TakeScreenshout是selenium工具自带的截图方法(截图类),这个类主要是获取浏览器窗体内的内容,不包括浏览器的菜单和桌面的任务栏区域

package org.seleniumhq.selenium;import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;
import java.util.Calendar;import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;/*** (1)访问度娘首页* (2)调用截图类截图* (3)保存截图*/
public class TakeScreenshot {public static void main(String[] args) throws Exception {System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");WebDriver driver = new ChromeDriver();driver.manage().window().maximize();driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);driver.get("https://www.baidu.com");Thread.sleep(1000);// 调用截图方法File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);/*** 截屏操作* 图片已当前时间命名*/SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //转换时间格式String time = dateFormat.format(Calendar.getInstance().getTime()); //获取当前时间try {// 拷贝截图文件到我们项目./ScreenshotsFileUtils.copyFile(src, new File("Screenshots", time + ".png"));Thread.sleep(3000);System.out.println("browser will be close");driver.quit();} catch (IOException e) {System.out.println(e.getMessage());}}
}

项目目录下自动创建文件夹

方式2:
Robot这个类java.awt.Robot。电脑整个屏幕的截图
项目下先创建文件夹RobotScreenshots

package org.seleniumhq.selenium;import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;import javax.imageio.ImageIO;import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;/**(1)访问搜狐首页(2)调用截图类截图(3)保存截图*/
public class RobotScreenShot {public static void main(String[] args) throws Exception {System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");WebDriver driver = new ChromeDriver();driver.manage().window().maximize();driver.get("https://www.sohu.com/");robotSnapshot();Thread.sleep(2000);System.out.println("browser will be close");driver.quit();}/*** 截屏方法二、Robot实现截屏* @throws Exception*/public static void robotSnapshot() throws Exception {//调用截图方法BufferedImage img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));ImageIO.write(img, "png", new File("RobotScreenshots","robot_screen01.png"));}}

方式3:
截取某个元素(或者目标区域)的图片。对图片进行指定坐标裁剪

package org.seleniumhq.selenium;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;/*
具体步骤就是:
(1)访问百度首页
(2)点击“设置”中的“搜索设置”
(3)调用截图类截图搜索设置页面
(4)保存截图(搜索设置页面)*/
public class ElementScreenShot {private static WebDriver driver;public static void main(String[] args) throws Exception {System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");driver = new ChromeDriver();driver.get("http://www.baidu.com");driver.manage().window().maximize();Thread.sleep(2000);WebElement setting = driver.findElement(By.id("s-usersetting-top"));Actions actions = new Actions(driver);actions.clickAndHold(setting).perform();   //点击左键不松开driver.findElement(By.linkText("搜索设置")).click();Thread.sleep(1000);WebElement xuanxiang = driver.findElement(By.xpath("/html/body/div[1]/div[6]/div"));File src = ((ChromeDriver) driver).getScreenshotAs(OutputType.FILE);try {FileUtils.copyFile(src, new File("D:\\screenshoot\\result.png"));FileUtils.copyFile(ElementScreenShot.captureElement(src, xuanxiang), new File("D:\\screenshoot\\test.png"));Thread.sleep(2000);System.out.println("browser will be close");driver.quit();} catch (IOException e) {e.printStackTrace();}}public static File captureElement(File screenshot, WebElement element){try {BufferedImage img = ImageIO.read(screenshot);int width = element.getSize().getWidth();int height = element.getSize().getHeight();//获取指定元素的坐标Point point = element.getLocation();//从元素左上角坐标开始,按照元素的高宽对img进行裁剪为符合需要的图片BufferedImage dest = img.getSubimage(point.getX(), point.getY(), width, height);ImageIO.write(dest, "png", screenshot);} catch (IOException e) {e.printStackTrace();}return screenshot;}}

java+ selenium截图相关推荐

  1. java selenium div内嵌滚动条 网页长截图发邮件

    java selenium 网页内嵌滚动条截图发邮件 主要问题 下面展开说 由于公司要求做一个接口,请求这个接口进行网页截图并发送邮件的功能,本来前期是用python写好了,but似乎不太符合要求,那 ...

  2. Java Selenium起步

    先将录制的脚本导出来,转成java的格式 在Eclipse中编辑并运行test     2.1 新建一个Java project: File-New-Java Project     2.2 在上一步 ...

  3. java selenium iframe_java selenium处理Iframe中的元素示例

    java selenium  处理Iframe 中的元素 有时候我们定位元素的时候,发现怎么都定位不了. 这时候你需要查一查你要定位的元素是否在iframe里面 阅读目录 什么是iframe ifra ...

  4. java零碎总结---java实现截图并保存到本地

    java零碎总结---java实现截图并保存到本地             1.java实现截图并保存到本地 提供给大家三个方法,也是整理的,都不是很难,大家需要的看看哈 2.原理都相似 ------ ...

  5. Java+selenium之WebDriver的抛出异常分析(七)

    Java+selenium之WebDriver的抛出异常分析(七) 参考文章: (1)Java+selenium之WebDriver的抛出异常分析(七) (2)https://www.cnblogs. ...

  6. Java+Selenium 3.x 实现Web自动化 - 1.自动化准备

    (一)自动化准备 说明:本文主要记录了基于公司现有项目(一个电子商务平台),从0开始实现UI自动化的历程.从准备阶段,部分内容直接省略了基础知识,一切以最终做成自动化项目为目标,难免会有晦涩之处.文章 ...

  7. 运用js解决java selenium元素定位问题

    运用js解决java selenium元素定位问题 参考文章: (1)运用js解决java selenium元素定位问题 (2)https://www.cnblogs.com/limxiaosi/p/ ...

  8. Java selenium和python_c#和Java和python设置selenium超时时间

    c# //设置页面加载超时时间 this.driver.Manage().Timeouts().PageLoad.Seconds.Equals(TimeSpan.FromSeconds(300)); ...

  9. java实现截图功能

    java实现截图功能 java实现截图/录屏 public static void main(String[] args) throws InterruptedException {//截取整个屏幕 ...

  10. java + Selenium实现12306自动购票

    为什么搞这个东西?[java + Selenium实现12306自动购票, 余票监测] 1.主要是12306是爬虫界的一个分水岭,所以我一直想玩12306[本次的实现并非真正意义上的破解12306实现 ...

最新文章

  1. 重力感应机制和手机的屏幕绘画
  2. 怎么隐藏滚动条又能滚动
  3. 数学建模 整数规划的基本原理和标准形式
  4. php file del 方法,php怎么遍历文件删除指定字符
  5. 25个关键技术点,带你熟悉Python
  6. android studio下生成aar文件,本地调用
  7. python实验教程_python语言程序设计实践教程实验七
  8. aix系统服务器限制端口访问,aix系统怎么查看端口是否开启
  9. weblogic安装与部署项目
  10. 没人教的项目管理方法之(练好你的站桩) 一、 干系人分析应该怎么做
  11. 苹果Mac 软件出现意外退出解决方法
  12. 基于python的掺杂介质六面体nastran网格生成脚本
  13. android 程序数据目录,Android APP 数据存放目录
  14. HBaseCon Asia 2019 Track 3 概要回顾
  15. 百度地图常用的几个webAPI(中文地址转经纬度,经纬度转中文地址)
  16. 如何使用数据库的SQLServer身份验证登陆
  17. html5手机号输入框,input输入框限制(座机,手机号码)
  18. 翼方健数CEO罗震——推动区域级数据互联互通的先行者
  19. 追光的人终会光芒万丈
  20. C语言用数组计算成绩平均值,C程序使用数组计算平均值

热门文章

  1. C#获取电脑硬盘序列号
  2. java jco sap 重连_Java连接SAP,使用SAPJCO3.jar
  3. linux表白程序源码,程序员表白程序,开放源码在此!
  4. springboot+vue3+elementui plus进销存源码
  5. 完成端口IOCP详解
  6. 皮尔逊相关系数(Pearson Correlation Coefficient)
  7. k3 梅林固件设置_OpenWrt中,旁路由的设置与使用
  8. 城市场景车路协同网络需求研究
  9. H3C交换机配置的备份与恢复[3CDaemon]
  10. 3CDaemon FTP使用教程