使用chromedriver抓取网页截图
前提:
1、电脑安装了谷歌浏览器
2、下载chromedriver-win64,放到C:\Program Files\Google\Chrome\chromedriver-win64 安装路径
chromedriver-win64 下载地址:Chrome for Testing availability
3、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws InterruptedException, IOException { WebDriver driver = null ; try { System.setProperty( "webdriver.chrome.driver" , "C:\\Program Files\\Google\\Chrome\\chromedriver-win64\\chromedriver.exe" ); ChromeOptions options = new ChromeOptions(); options.addArguments( "--headless" ); // 无界面模式 options.addArguments( "--hide-scrollbars" ); // 隐藏滚动条(可选) options.addArguments( "--force-device-scale-factor=2" ); // 设置缩放因子为 2,提高清晰度 driver = new ChromeDriver(options); driver.get( "https://www.baidu.com/" ); // 替换为你想截图的网页 Thread.sleep( 20000 ); File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); File imageFile = new File( "D:\\笔记\\test.png" ); FileUtils.copyFile(screenshot, imageFile); // 保存截图 } finally { if (driver != null ){ driver.quit(); } } } } |
效果图(会看到看到明显截图是不全的。)是因为设置了缩放因子,提高清晰度导致的
如何处理这种情况呢。
A、添加一个模拟滚动调整
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /** * 模拟滚动条 * @param driver * @throws InterruptedException */ public static void analogScrollBar(WebDriver driver) throws InterruptedException { // 模拟滚动条,解决懒加载问题 String jsHeight = "return document.body.clientHeight" ; long heightIndex = ( long ) ((JavascriptExecutor) driver).executeScript(jsHeight); long k = 1 ; // 模拟手动滚动条 while (k * 500 < heightIndex) { String jsMove = "window.scrollTo(0, " + (k * 500 ) + ")" ; System.out.println(jsMove); ((JavascriptExecutor) driver).executeScript(jsMove); Thread.sleep( 200 ); heightIndex = ( long ) ((JavascriptExecutor) driver).executeScript(jsHeight); k++; } // 获取网页的宽度和高度 String jsWidth = "return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);" ; String jsHeightFull = "return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);" ; long widthIndex = ( long ) ((JavascriptExecutor) driver).executeScript(jsWidth); heightIndex = ( long ) ((JavascriptExecutor) driver).executeScript(jsHeightFull); System.out.println( "Width: " + widthIndex + ", Height: " + heightIndex); // 设置浏览器窗口大小 driver.manage().window().setSize( new Dimension(( int ) widthIndex + 100 , ( int ) heightIndex + 500 )); } |
效果图:
B、去掉缩放因子(A方式比较,这样截出来的图片会更加清晰)
分类:
JAVA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-11-07 SWT如何获取Shell对象