开发手册 欢迎您!
软件开发者资料库

Java Selenium(Chrome)载取滚动条网页长图的方法及示例代码

本文主要介绍Java使用Selenium操作Chrome等浏览器,载取带有滚动条的网页,载取网页完整长图的方法及示例代码。

1、下载指定版本Driver驱动

Firefox浏览器驱动geckodriver
Chrome浏览器驱动ChromeDriver
IE浏览器驱动IEDriverServer
Edge浏览器驱动MicrosoftWebDriver
Opera浏览器驱动operadriver
PhantomJS浏览器驱动phantomjs

2、安装引用Selenium

1) 下载Jar包

下载地址http://docs.seleniumhq.org/download/

2) Maven安装



org.seleniumhq.selenium
selenium-java
3.4.0

2、完整页面截图

import java.io.File;import java.io.IOException;import java.util.concurrent.TimeUnit;import javax.imageio.ImageIO;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.testng.annotations.Test;import ru.yandex.qatools.ashot.AShot;import ru.yandex.qatools.ashot.Screenshot;import ru.yandex.qatools.ashot.shooting.ShootingStrategies;public class EntireScreenshot {    public static void main(String[] args) {        // TODO Auto-generated method stub    WebDriver driver;                System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");            driver=new ChromeDriver();            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);            driver.manage().window().maximize();driver.get("https://www.wonhero.com/article/411/");          / /take screenshot of the entire page            Screenshot screenshot=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);            try {                ImageIO.write(screenshot.getImage(),"PNG",new File("path of the file"));            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            driver.quit();        }    }

3、载取网页长图

改变窗口尺寸来截长图

import java.io.File;
import java.io.IOException;import java.util.concurrent.TimeUnit;import javax.imageio.ImageIO;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.testng.annotations.Test;import ru.yandex.qatools.ashot.AShot;import ru.yandex.qatools.ashot.Screenshot;import ru.yandex.qatools.ashot.shooting.ShootingStrategies;public class EntireScreenshot {public static void main(String[] args) {// TODO Auto-generated method stubWebDriver driver;System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");driver = new ChromeDriver();try {driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);driver.manage().window().maximize();driver.get("https://www.wonhero.com/article/411/"); //等待页面加载完成new WebDriverWait(driver, 300).until(driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete"));JavascriptExecutor jexec = (JavascriptExecutor) driver;int width = (int) jexec.executeScript("return document.body.scrollWidth");int height = (int) jexec.executeScript("return document.body.scrollHeight"); //设置浏览窗口大小driver.manage().window().setSize(new Dimension(width, height));Screenshot screenshot = new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver);BufferedImage image = screenshot.getImage();ImageIO.write(image, "PNG", new File("D:\\temp\\" + "AShot_BBC_Entire.png"));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}driver.quit();}}

Maven引用依赖(ashot):


ru.yandex.qatools.ashot
ashot
1.5.4

相关文档:

java selenium WebDriver中executeAsyncScript和executeScript方法的使用

java selenium WebDriver操作调用浏览器后台执行Js(javaScript)代码

java 通过selenium WebDriver和命令行方式使用PhantomJS方法及示例代码