实现给页面长截图,带滚动条的部分也截取

问题描述

页面出现滚动条的部分也需要截取,类似于 QQ里面的长截图,要求截取一整张完整页面


解决方案:

利用谷歌浏览器命令

首先用谷歌浏览器打开页面

看一下键盘左上角ESC键是否亮灯,如果没有,按左下角fn键 + esc 绿灯亮;

然后按f12或者右键检查,出现

 然后ctrl + shift + p,出现运行命令框,有的是英文有的是中文,中文搜索"截取完整尺寸的屏幕截图",英文搜索 "Capture full size screenshot",然后页面左下角就会出现下载好的图片。

 

 

在Java中,如果要截取页面中未显示的部分,可以使用一些第三方库来模拟浏览器操作,如Selenium WebDriver和Headless Chrome。 首先,确保已经配置好浏览器驱动(如ChromeDriver)并将其路径设置为系统属性。 然后,可以使用以下代码片段来实现页面截图,包括未显示的部分: ```java import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; import ru.yandex.qatools.ashot.shooting.ShootingStrategies; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; public class FullPageScreenshot { public static void main(String[] args) { // 设置浏览器驱动路径 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // 创建 ChromeOptions 实例 ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); // 设置为无界面模式,即后台运行 // 创建 DesiredCapabilities 实例 DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, options); // 创建 ChromeDriver 实例 WebDriver driver = new ChromeDriver(capabilities); // 最大化浏览器窗口 driver.manage().window().maximize(); // 打开页面 driver.get("https://example.com"); // 使用 JavaScript 获取页面高度 long pageHeight = (Long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight"); // 创建 AShot 实例 AShot ashot = new AShot(); // 设置截图选项,包括滚动条截图 ashot.shootingStrategy(ShootingStrategies.viewportPasting(1000).takeFullPage()); // 截取整个页面截图,包括未显示的部分 Screenshot screenshot = ashot.takeScreenshot(driver); try { // 保存截图为文件 ImageIO.write(screenshot.getImage(), "PNG", new File("path/to/screenshot.png")); } catch (IOException e) { e.printStackTrace(); } // 关闭浏览器 driver.quit(); } } ``` 上述代码中,首先设置了浏览器驱动路径,并创建了一个ChromeOptions实例,将其设置为无界面模式。然后,创建一个DesiredCapabilities实例,并将ChromeOptions添加到capabilities中。 接着,创建一个ChromeDriver实例,并最大化浏览器窗口。通过`driver.get("https://example.com")`打开指定的页面。 使用JavaScript执行`document.body.scrollHeight`获取页面的高度。 创建AShot实例后,通过`ashot.shootingStrategy()`方法设置截图选项,其中`ShootingStrategies.viewportPasting(1000).takeFullPage()`表示截取整个页面,包括未显示的部分。 最后,使用`ashot.takeScreenshot(driver)`进行页面截图,并将截图保存为文件。 记得将代码中的路径替换为实际的路径,运行代码即可获得包括未显示部分页面截图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值