截屏操作__Robot对象__截取当前分辨率的截图

Posted 18513757531

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了截屏操作__Robot对象__截取当前分辨率的截图相关的知识,希望对你有一定的参考价值。

一、代码如下

package www.gui.huohu.pom;

import java.awt.AWTException;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Capture2 {
	WebDriver driver= null;
	Actions actions= null;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Capture2 gx= new Capture2();
		gx.init();

	}
	
	private void init() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		// System.setProperty("webdriver.firefox.bin", "D:\\Mozilla
		// Firefox\\firefox.exe");
		System.setProperty("webdriver.firefox.bin", "D:\\FireFox__37\\firefox.exe");

		this.driver = new FirefoxDriver();// 启动火狐浏览器

		this.driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);// 元素等待时间
		this.driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);// 页面加载时间
		this.driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);// ajax请求
		this.driver.manage().window().maximize();
		// this.driver.get("https://www.baidu.com/");// 在浏览器中打开该网址
		actions = new Actions(driver);
		
		this.driver.get("http://localhost/Agileone/");
		this.driver.findElement(By.id("username")).sendKeys("admin");
		this.driver.findElement(By.id("password")).sendKeys("admin");
		this.driver.findElement(By.id("login")).click();
		this.driver.findElement(By.partialLinkText("需求提案")).click();
		this.driver.findElement(By.partialLinkText("缺陷跟踪")).click();
		
		
		int[] wh= new Common().getScreenWH();
		Rectangle screenRect= new Rectangle(0, 0, wh[0], wh[1]);
		try {
			BufferedImage bi= new Robot().createScreenCapture(screenRect);
		    ImageIO.write(bi, "png", new File("e:\\4445.png"));
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
	
	

}

 

 

package www.gui.huohu.pom;

import java.awt.Toolkit;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Common {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Common c= new Common();
		c.getScreenWH();
	}
	
	private void sleepSecond(int n) {
		// TODO Auto-generated method stub
		try {
			Thread.sleep(n*1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public int[] getScreenWH() {
		// TODO Auto-generated method stub
		int height= 0;
		int width= 0;
		height= (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		width= (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
//		System.out.println(height+"__"+width);
		int[] result= {width, height};
		return result;
	}

}

 

以上是关于截屏操作__Robot对象__截取当前分辨率的截图的主要内容,如果未能解决你的问题,请参考以下文章

截屏操作___senium__TakeScreenshot对象

AVPlayer 截取视频流的当前帧

Driver对象的navigate方法__浏览器被操作__前进后退刷新当前窗口切换到连接

iOS_截屏并裁剪

GUI__文件上传__robot对象输入文件链接__代码未实现

AWT Robot - 仅在 MacOS 上截取当前程序的屏幕截图