【问题标题】:C# - Get all open browsing tabs in all instances of firefoxC# - 获取所有 firefox 实例中所有打开的浏览选项卡
【发布时间】:2017-03-18 23:54:31
【问题描述】:

How can I get URLs of open pages from Chrome and Firefox?

How can I get URLs of open pages from Chrome and Firefox?

http://hintdesk.com/c-automationelement-left-click-and-send-keys/

How can I check if website is already opened in a webbrowser?

等等……

全部失败。它们要么只返回打开次数最多的标签,要么根本不工作。

我需要检查firefox是否完全打开到一个url,如果是则刷新并聚焦该选项卡/url,如果没有打开firefox到该页面。

这不是一个 Firefox 插件或任何与 Firefox 真正有关的东西。那是我选择的浏览器,所以这是我想要使用的浏览器。所有这些都是为了避免每次运行我的应用程序时连续打开相同的 url。

【问题讨论】:

    标签: c# firefox


    【解决方案1】:

    您可以使用 System.Runtime.InteropServices 打印窗口名称。

    [DllImport("user32.dll")]
    static extern int GetWindowTextLength(IntPtr hWnd);
    
    [DllImport("user32.dll")]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
    
    public static void PrintBrowserTabName()
    {
        var browsersList = new List<string>
        {
            "chrome",
            "firefox",
            "iexplore",
            "safari",
            "opera",
            "edge"
        };
    
        foreach (var singleBrowser in browsersList)
        {
            var process = Process.GetProcessesByName(singleBrowser);
            if (process.Length > 0)
            {
                foreach (Process singleProcess in process)
                {
                    IntPtr hWnd = singleProcess.MainWindowHandle;
                    int length = GetWindowTextLength(hWnd);
    
                    StringBuilder text = new StringBuilder(length + 1);
                    GetWindowText(hWnd, text, text.Capacity);
                    Console.WriteLine(text.ToString());
                }
            }
        }
    }
    

    【讨论】:

    • 在 Chrome 上,它只返回当前活动标签的标题。在 Edge 上相同(名称更改为“msedge”)
    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2017-12-15
    • 2011-09-27
    相关资源
    最近更新 更多