目录
效果
详细效果 图片太大上传不了
依赖
<!--网页自动化测试 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
火狐驱动
稍后更新上去linux 和 win的都带上
download.csdn.net/download/we…
示例代码
引入
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import com.google.common.io.Files;
类
/**
* Screenshot 实现 浏览器截取页面转图片服务
*
* 可以实现截取全图能力
*
* 20200716
*
* @author yushen
*
*/
public class ScreenshotHtmlToImg {
}
运行main方法
public static void main(String[] args) throws InterruptedException {
long startMili = System.currentTimeMillis();// 开始时间
System.out.println("stak execute ready time"+startMili);
//网页地址
String httpurl = "https://uland.taobao.com/sem/tbsearch?refpid=mm_26632258_3504122_32538762&keyword=%E5%A5%B3%E8%A3%85&clk1=8594240c35c5fbb1ee0ae2f1d7ef0c0f&upsid=8594240c35c5fbb1ee0ae2f1d7ef0c0f";
// String httpurl = "http://www.baidu.com"
//文件存放地址
String fileUrl = "d:/test/000000000000000000092screenfile.png";
Boolean b = firefoxJT(httpurl,fileUrl);
long endMili = System.currentTimeMillis(); // 结束时间
System.out.println("stak execute "+b+" total time:" + (endMili - startMili) / 1000 + " s");
}
具体执行任务方法
/**
* Screenshot 调用 火狐浏览器 驱动,实现截取页面转图片
*
* @param httpUrl //网页地址
* @param outputFileUrl //文件存放地址
*/
public static Boolean firefoxJT(String httpUrl, String outputFileUrl) {
try {
String projectPath = System.getProperty("user.dir");
// System.out.println("projectPath==" + projectPath);
// selenium3需要加载firefox的geckodriver.exe
String path = projectPath + "/firefoxdriver/geckodriver-v0.26.0-win64/geckodriver.exe";
// 设置firefox的系统属性
System.setProperty("webdriver.gecko.driver", path);
WebDriver driver = new FirefoxDriver();
// driver.get("http://www.baidu.com");
// Long is = (Long) ((FirefoxDriver) driver).executeScript("return window.document.body.scrollHeight;");
// System.out.println(is);
driver.get(httpUrl);
//获取页面内容 高度
Long scrollHeight = (Long) ((FirefoxDriver) driver).executeScript("return window.document.body.scrollHeight;");
// System.out.println(is1);
int scrollHeightInt = scrollHeight.intValue();
System.out.println("页面高度:"+scrollHeightInt);
// driver.manage().window().maximize();
//
// Dimension s = driver.manage().window().getSize();
// System.out.println(s.getHeight()+"---"+s.getWidth());
// JavascriptExecutor j = (JavascriptExecutor) driver;
// j.executeScript("alert(window.scrollY)");
// Alert alert = driver.switchTo().alert();
//
// String text = alert.getText();
// System.out.println(text);
//
// JavascriptExecutor jse = (JavascriptExecutor)driver;
// String heightMax = (String) jse.executeScript("var heightmax ='12312312'; return heightmax;");
// System.out.println("浏览器高度:"+heightMax);
// String heightMax = (String) jse.executeScript("var heightmax = window.scrollY; return heightmax;");
// System.out.println("浏览器高度:"+heightMax);
// Thread.sleep(5000);
//TODO 之后研究下如何获取页面总高度 把下边
// 设置窗体大小宽高
driver.manage().window().setSize(new Dimension(1366, scrollHeightInt+500));
Thread.sleep(3000);
// 等待三秒加载图片
// 指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,其含义是将截取的屏幕以文件形式返回。
// driver需要强制转换为RemoteWebDriver对象,可以通过eclipse提示进行处理
File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
try {
// 利用FileUtils工具类的copy()方法保存getScreenshotAs()返回的文件对象。
// 看到网上有使用File.copyFile()方法,我这里测试的结果需要使用copy()方法
Files.copy(scrFile, new File(outputFileUrl));
} catch (IOException e) {
// 异常处理
e.printStackTrace();
System.out.println(e.toString());
}
driver.quit();
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
return false;
}
}
ok 持续更新