使用selenium+phantomjs实现对指定页面的指定元素截图(包含隐藏区域)

一.引入类库,pom.xml文件配置如下

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.2.0</version>
</dependency>

二.下载phantomjs驱动

selenium中也可以通过使用firefoxDriver或chromeDriver来实现指定元素截图功能,但是只能对当前屏幕进行截图,屏幕之外的区域(滚动条下面的区域)无法截取,所以这里选择了phantomjs(支持多种平台),selenium类库中包含部分的phantomjs,可以基于此来实现自身需要的功能。

phantomjs的下载地址如下:http://phantomjs.org/download.html或者http://npm.taobao.org/dist/phantomjs/

三.编写截图工具类CaptureUtil.java,代码如下

package com.chinobot.capture.utils;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RasterFormatException;
import java.text.SimpleDateFormat;
import java.io.*;
import java.util.*;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

public class CaptureUtil {
    private final static ResourceBundle resourceBundle=ResourceBundle.getBundle("application");

    //需要截图的url地址
    private final static String captureUrl=resourceBundle.getString("capture.captureUrl");

    //需要截图元素className
    private final st
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值