selenium页面元素截图
2016-08-26 12:04
417 查看
http://blog.csdn.net/fgwvip123/article/details/51671279
[java] view
plain copy
package com.selenium.api;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
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.internal.WrapsDriver;
public class Util {
//页面元素截图
public static File captureElement(WebElement element) throws Exception {
WrapsDriver wrapsDriver = (WrapsDriver) element;
// 截图整个页面
File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read(screen);
// 获得元素的高度和宽度
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
// 创建一个矩形使用上面的高度,和宽度
Rectangle rect = new Rectangle(width, height);
// 得到元素的坐标
Point p = element.getLocation();
BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,rect.height);
//存为png格式
ImageIO.write(dest, "png", screen);
return screen;
}
@Test
public void testCaptureElement(){
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.baidu.com");
WebElement wb = driver.findElement(By.id("su"));
try {
FileUtils.copyFile(captureElement(wb), new File("c:\\a.png"));
} catch (Exception e) {
e.printStackTrace();
}
driver.quit();
}
}
[java] view
plain copy
package com.selenium.api;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
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.internal.WrapsDriver;
public class Util {
//页面元素截图
public static File captureElement(WebElement element) throws Exception {
WrapsDriver wrapsDriver = (WrapsDriver) element;
// 截图整个页面
File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read(screen);
// 获得元素的高度和宽度
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
// 创建一个矩形使用上面的高度,和宽度
Rectangle rect = new Rectangle(width, height);
// 得到元素的坐标
Point p = element.getLocation();
BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,rect.height);
//存为png格式
ImageIO.write(dest, "png", screen);
return screen;
}
@Test
public void testCaptureElement(){
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.baidu.com");
WebElement wb = driver.findElement(By.id("su"));
try {
FileUtils.copyFile(captureElement(wb), new File("c:\\a.png"));
} catch (Exception e) {
e.printStackTrace();
}
driver.quit();
}
}
相关文章推荐
- Python + Selenium 实现对页面的指定元素截图(可截长图元素)【转载】
- Selenium学习8--截图,拖拽页面元素,键盘操作,鼠标右键,悬停,鼠标双击
- selenium页面元素截图
- selenium之如何使用cssSelector定位页面元素
- 解决关于selenium 获取新页面元素失败的问题
- Selenium2学习-025-WebUI自动化实战实例-023-页面快照截图应用之一 -- 常规截图(全页面)
- selenium webdriver学习-怎么等待页面元素加载完成
- 页面元素定位 id 和 xpath 使用selenium IDE 浏览器插件获取
- selenium webdriver(Appium)如何等待页面元素加载完成
- Selenium with Python 005 - 调用js操作页面元素
- Python+Selenium练习篇之22-获取页面元素大小
- selenium高效应对Web页面元素刷新的实例讲解
- selenium webdriver xpath 定位页面元素
- selenium 常见方法和页面元素的操作
- 想点击页面上的File菜单, 但是用selenium怎么抓多层DIV里的路径呢?每次执行的时候总是报找不到元素
- selenium抓取页面可用元素css
- selenium2.0关于python的常用函数(二) 定位元素,解析页面时用到
- Selenium操作页面元素
- Selenium with Python 004 - 页面元素操作
- selenium webdriver学习(四)------------定位页面元素(转)