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

如何用Selenium和C#捕获含网址栏与标签页的浏览器截图

捕获包含浏览器网址栏和标签页的截图方案(C# + Selenium 4.18.1)

Selenium原生的ITakesScreenshot接口仅能捕获网页内容区域,无法包含浏览器的网址栏、标签页等UI元素。要实现目标效果,需要通过捕获整个浏览器窗口的方式来实现,以下是两种可行方案:

方案1:Windows API捕获浏览器窗口(Windows环境专属)

通过调用Windows系统API获取浏览器窗口句柄并捕获整个窗口图像,是Windows环境下最直接的实现方式。

实现步骤与代码

  1. 确保项目引用依赖:.NET Framework需引用System.Drawing;.NET Core/.NET 5+需安装NuGet包System.Drawing.Common。
  2. 编写API调用与截图逻辑:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using OpenQA.Selenium;

public class BrowserWindowScreenshot
{
    // Windows系统API声明
    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    [DllImport("user32.dll")]
    private static extern IntPtr GetWindowDC(IntPtr hWnd);

    [DllImport("gdi32.dll")]
    private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport("gdi32.dll")]
    private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

    [DllImport("gdi32.dll")]
    private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

    [DllImport("user32.dll")]
    private static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, uint nFlags);

    [DllImport("gdi32.dll")]
    private static extern bool DeleteDC(IntPtr hdc);

    [DllImport("gdi32.dll")]
    private static extern bool DeleteObject(IntPtr hObject);

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

    public string TakeFullBrowserScreenshot(IWebDriver driver)
    {
        // 转换浏览器窗口句柄
        IntPtr windowHandle = new IntPtr(long.Parse(driver.CurrentWindowHandle));

        // 获取窗口边界尺寸
        GetWindowRect(windowHandle, out RECT windowRect);
        int width = windowRect.Right - windowRect.Left;
        int height = windowRect.Bottom - windowRect.Top;

        // 创建截图所需的设备上下文与位图
        IntPtr windowDc = GetWindowDC(windowHandle);
        IntPtr compatibleDc = CreateCompatibleDC(windowDc);
        IntPtr compatibleBitmap = CreateCompatibleBitmap(windowDc, width, height);
        IntPtr oldBitmap = SelectObject(compatibleDc, compatibleBitmap);

        // 捕获窗口图像
        PrintWindow(windowHandle, compatibleDc, 0);

        // 转换为Bitmap对象并处理
        Bitmap screenshot = Image.FromHbitmap(compatibleBitmap);
        string savePath = @"C:\projects\full_browser_screenshot.png";
        screenshot.Save(savePath, ImageFormat.Png);

        // 释放系统资源
        SelectObject(compatibleDc, oldBitmap);
        DeleteDC(compatibleDc);
        DeleteObject(compatibleBitmap);

        // 转换为Base64编码返回
        using (var ms = new System.IO.MemoryStream())
        {
            screenshot.Save(ms, ImageFormat.Png);
            return Convert.ToBase64String(ms.ToArray());
        }
    }
}

注意事项

  • 仅适用于Windows系统,依赖Windows专属API。
  • 主流浏览器(Chrome、Edge、Firefox)中,driver.CurrentWindowHandle均可正常获取窗口句柄。

方案2:跨平台第三方库捕获(多平台适配)

如果需要跨Windows、Linux、macOS运行,可使用跨平台截图库(如SkiaSharp),结合平台API获取浏览器窗口的位置与尺寸,再捕获对应区域的屏幕。

核心思路

  1. 安装SkiaSharp及对应平台的原生资产NuGet包(如SkiaSharp.NativeAssets.Windows、SkiaSharp.NativeAssets.Linux)。
  2. 通过平台特定API获取浏览器窗口的位置和尺寸(例如Linux用X11 API,macOS用Cocoa API)。
  3. 使用SkiaSharp捕获指定区域的屏幕图像。

注:跨平台实现需针对不同平台编写适配代码,复杂度较高,如果仅需Windows环境,优先选择方案1。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 02:33:20