You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Edge浏览器IE模式下Selenium仅捕获半屏,如何截取整页?

解决Edge IE模式下Selenium无法截取整页截图的问题

问题核心

Edge IE模式依赖IE驱动运行,而IE驱动原生对全屏截图的支持存在局限性,再加上配置疏漏、窗口未适配页面实际高度,导致仅能捕获半屏内容。

解决方案

1. 修正IE驱动配置错误

你的代码中System.setProperty的键存在语法错误,同时优化IE选项配置:

  • 移除webdriver.ie.driver键名里多余的逗号和空格
  • 确保takeFullPageScreenshot能力正确生效

2. 适配窗口大小为页面实际高度

浏览器最大化后,页面可能仍存在滚动内容,需获取页面实际滚动高度并调整窗口尺寸,保证截图覆盖全部内容。

3. 正确配置Ashot库(若继续使用)

若用Ashot未生效,需启用viewportPasting策略,自动拼接滚动区域的截图。

修改后的代码示例

修正后的Login方法

public static void Login() throws IOException, InterruptedException {
    // 修正System.setProperty的键名
    System.setProperty("webdriver.ie.driver", "C:\\IEDRIVER-64\\IEDriverServer.exe");
    InternetExplorerOptions ieOptions = new InternetExplorerOptions();
    ieOptions.attachToEdgeChrome();
    ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
    ieOptions.ignoreZoomSettings();
    ieOptions.setCapability("ignoreProtectedModeSettings", true);
    ieOptions.setCapability("requireWindowFocus", true);
    ieOptions.withInitialBrowserUrl("http://www.bing.com");
    // 启用全屏截图能力
    ieOptions.setCapability("takeFullPageScreenshot", true);

    driver = new InternetExplorerDriver(ieOptions);
    driver.manage().window().maximize();
    driver.get("http://www.bing.com");
    
    // 调整窗口高度为页面实际滚动高度
    JavascriptExecutor js = (JavascriptExecutor) driver;
    Long pageHeight = (Long) js.executeScript("return document.body.scrollHeight");
    Dimension currentSize = driver.manage().window().getSize();
    driver.manage().window().setSize(new Dimension(currentSize.width, pageHeight.intValue()));
    // 等待页面渲染完成
    Thread.sleep(1000);
}

修正后的原生Selenium截图方法

public static void CaptureScreesnhot() throws IOException {
    Date d = new Date();
    String filename = d.toString().replace(":", "_").replace(" ", "_") + ".png";
    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenshot, new File("你的文件路径" + filename));
}

Ashot库的正确使用配置

public static void CaptureFullPageWithAshot() throws IOException {
    Date d = new Date();
    String filename = d.toString().replace(":", "_").replace(" ", "_") + ".png";
    
    Screenshot screenshot = new AShot()
            .shootingStrategy(ShootingStrategies.viewportPasting(100)) // 滚动拼接,间隔100ms等待渲染
            .takeScreenshot(driver);
    
    ImageIO.write(screenshot.getImage(), "PNG", new File("你的文件路径" + filename));
}

注意事项

  • 确保IEDriverServer版本与Edge版本兼容,建议使用最新版驱动
  • 若页面包含动态加载内容,需延长等待时间,确保所有元素加载完成后再执行截图
  • 检查Edge IE模式的权限设置,避免权限限制导致截图异常

内容的提问来源于stack exchange,提问作者Ankita Hatwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 02:33:22