C#-在Firefox的所有实例中获取所有打开的浏览选项卡 [英] C# - Get all open browsing tabs in all instances of firefox

查看:48
本文介绍了C#-在Firefox的所有实例中获取所有打开的浏览选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取URL从Chrome和Firefox打开的页面?

如何获取URL从Chrome和Firefox打开的页面?

http://hintdesk.com/c-automationelement-左键并发送键/

如何检查是否网站已经在浏览器中打开了?

等...

全部失败.它们要么只返回最顶部打开的选项卡,要么根本不起作用.

all fail. Either they return only the top most opened tab or do not work at all.

我需要检查firefox是否完全打开了某个URL,然后刷新并集中显示该选项卡/URL,如果没有打开firefox,则指向该页面.

I need to check and see if firefox is opened at all to a url, if it is then refresh and focus that tab/url, if not open firefox to that page.

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

This is not a firefox addon or anything really having to do with firefox. That is my browser of choice so that is the one I desire to use. All this is to avoid consecutively opening the same url each time my app is run.

推荐答案

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

You can print window name using 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());
            }
        }
    }
}

这篇关于C#-在Firefox的所有实例中获取所有打开的浏览选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆