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

C#收集Edge浏览器PDF标签页路径:AutomationElement类引入问题

可以通过AutomationElement实现需求,解决步骤如下

1. 解决System.Windows.Automation命名空间引用问题

在VS 2022(.NET 8.0)中,该命名空间对应的程序集默认未被项目引用,需手动添加:

  • 右键项目 → 选择「添加」→「引用」
  • 在「程序集」→「框架」列表中,找到并勾选 UIAutomationClient 和 UIAutomationTypes
  • 点击「确定」后,即可在代码中引入命名空间:using System.Windows.Automation;

也可以通过NuGet安装包 System.Windows.Automation 快速添加依赖。

2. 实现代码示例

以下代码可遍历Edge所有标签页,收集本地PDF路径并弹窗展示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Automation;
using System.Diagnostics;
using System.Windows.Forms;

namespace EdgePdfCollector
{
    class Program
    {
        static void Main(string[] args)
        {
            // 获取所有Edge主窗口进程
            var edgeProcesses = Process.GetProcessesByName("msedge").Where(p => p.MainWindowHandle != IntPtr.Zero);
            var pdfPaths = new HashSet<string>();

            foreach (var process in edgeProcesses)
            {
                AutomationElement edgeWindow = AutomationElement.FromHandle(process.MainWindowHandle);
                if (edgeWindow == null) continue;

                // 查找标签栏容器(若Edge版本更新导致失效,需用Inspect工具查看最新元素属性调整条件)
                Condition tabBarCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab);
                AutomationElement tabBar = edgeWindow.FindFirst(TreeScope.Descendants, tabBarCondition);
                if (tabBar == null) continue;

                // 遍历所有标签页
                AutomationElementCollection tabs = tabBar.FindAll(TreeScope.Children, Condition.TrueCondition);
                foreach (AutomationElement tab in tabs)
                {
                    // 获取标签页对应的文档窗口
                    AutomationElement tabDocument = tab.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
                    if (tabDocument == null) continue;

                    // 获取地址栏内容
                    if (tabDocument.TryGetCurrentPattern(ValuePattern.Pattern, out object patternObj))
                    {
                        ValuePattern valuePattern = (ValuePattern)patternObj;
                        string url = valuePattern.Current.Value;

                        // 筛选本地PDF路径
                        if (url.StartsWith("file://") && url.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
                        {
                            // 转换为标准本地路径
                            string localPath = Uri.UnescapeDataString(url.Replace("file:///", ""));
                            pdfPaths.Add(localPath);
                        }
                    }
                }
            }

            // 弹窗展示结果
            if (pdfPaths.Any())
            {
                MessageBox.Show(string.Join(Environment.NewLine, pdfPaths), "收集到的PDF本地路径");
            }
            else
            {
                MessageBox.Show("未找到打开的PDF标签页");
            }
        }
    }
}

注意事项

  • Edge的UI自动化元素结构可能随版本更新变化,若代码无法定位标签栏或地址栏,需使用Windows SDK自带的Inspect工具查看最新元素属性,调整查找条件。
  • 需确保程序以足够权限运行,避免因权限不足无法访问Edge窗口元素。
  • 控制台项目需额外添加 System.Windows.Forms 引用,可通过添加程序集引用或NuGet包完成。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:42:33