C# - 获取所有firefox实例中的所有打开浏览选项卡

时间:2016-11-05 04:12:03

标签: c# firefox

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,如果是,则刷新并关注该tab / url,如果没有打开firefox到该页面。

这不是一个firefox插件或任何与firefox有关的东西。这是我选择的浏览器,因此我希望使用它。所有这些都是为了避免每次运行我的应用程序时连续打开相同的URL。

1 个答案:

答案 0 :(得分:0)

您可以使用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)
        {
            IntPtr hWnd = singleProcess.MainWindowHandle;
            int length = GetWindowTextLength(hWnd);

            StringBuilder text = new StringBuilder(length + 1);
            GetWindowText(hWnd, text, text.Capacity);
            Console.WriteLine(text.ToString());
        }
    }
}