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

Windows桌面应用如何获取其他进程中浏览器窗口的当前URL?

针对Chromium内核浏览器的当前URL获取方案(Windows平台)

Windows平台没有像macOS AppleScript那样统一的系统级自动化接口,但针对Chromium内核浏览器(Chrome、Edge、Brave等),有两种可行的实现方式:

一、利用Chromium远程调试协议(通用方案)

所有Chromium系浏览器都支持远程调试协议,这是最通用的方法,步骤如下:

  1. 配置浏览器启动参数:给浏览器快捷方式添加启动参数,例如Chrome/Edge添加 --remote-debugging-port=9222,确保浏览器启动时开启调试端口。
  2. 通过HTTP请求获取标签信息:向http://localhost:9222/json发送GET请求,返回的JSON数据包含所有标签的状态(是否激活)、URL等信息。
  3. 解析JSON提取目标URL:过滤出active为true的标签,读取其url字段。

C#示例代码

using System;
using System.Net.Http;
using System.Text.Json;

class ChromiumUrlFetcher
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        const int DebugPort = 9222;
        using var httpClient = new HttpClient();
        
        try
        {
            var jsonResponse = await httpClient.GetStringAsync($"http://localhost:{DebugPort}/json");
            var tabs = JsonSerializer.Deserialize<ChromiumTab[]>(jsonResponse);

            foreach (var tab in tabs)
            {
                if (tab.active)
                {
                    Console.WriteLine($"当前激活标签URL: {tab.url}");
                    return;
                }
            }
            Console.WriteLine("未找到激活的标签");
        }
        catch (HttpRequestException)
        {
            Console.WriteLine("浏览器未开启远程调试端口,请检查启动参数");
        }
    }

    // 仅定义所需字段,其余字段可按需添加
    private class ChromiumTab
    {
        public bool active { get; set; }
        public string url { get; set; }
    }
}

注意事项

  • 若同时运行多个同内核浏览器,需指定不同的调试端口(如--remote-debugging-port=9223)避免冲突;
  • 部分浏览器的隐私模式可能会禁用远程调试,需提前告知用户。

二、Windows UI自动化(无需浏览器配置)

利用Windows系统提供的UI自动化(UIA)框架,可直接遍历浏览器窗口的控件结构,读取地址栏内容。此方法无需修改浏览器启动参数,但需针对不同浏览器的控件属性做适配。

C#示例代码(以Chrome为例)

using System;
using System.Windows.Automation;

class UiaUrlFetcher
{
    static void Main(string[] args)
    {
        // 查找Chrome主窗口(可根据进程名或窗口标题调整匹配条件)
        var desktopRoot = AutomationElement.RootElement;
        var chromeWindowCondition = new PropertyCondition(
            AutomationElement.NameProperty, 
            "Google Chrome", 
            PropertyConditionFlags.IgnoreCase
        );
        var chromeWindow = desktopRoot.FindFirst(TreeScope.Children, chromeWindowCondition);

        if (chromeWindow == null)
        {
            Console.WriteLine("未找到Chrome窗口");
            return;
        }

        // 查找地址栏控件(Chromium系浏览器地址栏的AutomationID通常为"address bar")
        var addressBarCondition = new PropertyCondition(
            AutomationElement.AutomationIdProperty, 
            "address bar"
        );
        var addressBar = chromeWindow.FindFirst(TreeScope.Descendants, addressBarCondition);

        if (addressBar != null)
        {
            if (addressBar.TryGetCurrentPattern(ValuePattern.Pattern, out var patternObj))
            {
                var valuePattern = patternObj as ValuePattern;
                Console.WriteLine($"当前URL: {valuePattern.Current.Value}");
            }
        }
        else
        {
            Console.WriteLine("未找到地址栏控件");
        }
    }
}

注意事项

  • 不同浏览器的控件属性可能略有差异,需实际测试调整匹配条件;
  • 应用需具备足够权限访问其他进程的UI元素,若运行在高权限环境下可能需要特殊处理;
  • 浏览器处于全屏、隐私模式时,控件结构可能发生变化,需额外适配。

总结

  • 优先选择远程调试协议,通用性强、可靠性高,适合大多数场景;
  • UI自动化适合无法要求用户配置浏览器的场景,但需要做浏览器适配;
  • Windows平台没有类似AppleScript的统一自动化接口,需针对Chromium系浏览器的特性实现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 06:36:15