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安装
<!-- selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
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.cjavapy.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 stub WebDriver 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.cjavapy.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 block e.printStackTrace(); } driver.quit(); } }
Maven引用依赖(ashot):
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>
相关文档:
java selenium WebDriver中executeAsyncScript和executeScript方法的使用