Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试列出浏览器chrome i'的所有打开选项卡时;i’我遇到了一个异常,我该怎么解决呢?_C#_.net_Ui Automation - Fatal编程技术网

C# 尝试列出浏览器chrome i'的所有打开选项卡时;i’我遇到了一个异常,我该怎么解决呢?

C# 尝试列出浏览器chrome i'的所有打开选项卡时;i’我遇到了一个异常,我该怎么解决呢?,c#,.net,ui-automation,C#,.net,Ui Automation,我在Program.cs top中有一个新项目我有: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Automation; using System.Windows.Forms; using System

我在Program.cs top中有一个新项目我有:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using System.Windows.Forms;
using System.Threading;

namespace List_all_opened_tabs_of_Firefox
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> firefoxUrls = new List<string>();

            Process firefox = Process.GetProcessesByName("chrome")[0];
            AutomationElement rootElement = AutomationElement.FromHandle(firefox.MainWindowHandle);
hwnd不能为IntPtr.Zero或null

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=hwnd cannot be IntPtr.Zero or null.
  Source=UIAutomationClient
  StackTrace:
       at System.Windows.Automation.AutomationElement.FromHandle(IntPtr hwnd)
       at List_all_opened_tabs_of_Firefox.Program.Main(String[] args) in c:\listopentabs\List all opened tabs of Firefox\List all opened tabs of Firefox\Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
第20行是:

AutomationElement rootElement = AutomationElement.FromHandle(firefox.MainWindowHandle);
我正在尝试获取所有打开的chrome选项卡的列表。 从此处下载并尝试了源代码:


在任务管理器中,有4个chrome进程正在运行。因此,在它们之间循环并选择一个非零的
MainWindowHandle

        var cp = Process.GetProcessesByName("chrome");
        IntPtr ww = IntPtr.Zero;
        foreach (var p in cp) {
            if (p.MainWindowHandle != IntPtr.Zero) {
                ww = p.MainWindowHandle;
            }
        }

我现在有一个chrome窗口,在这个窗口中我打开了30个标签。仅供参考chrome是多处理的。您可能已获得其中一个选项卡进程。从文档:“只有当流程具有图形界面时,流程才会有与其关联的主窗口。如果关联的进程没有主窗口,则MainWindowHandle值为零。对于已隐藏的进程(即任务栏中不可见的进程),该值也为零。在任务栏的最右边,在通知区域中出现图标的过程就是这样。“讨厌这会给我一个chrome的主窗口,我需要一个chrome窗口所有打开标签的列表。这就是我尝试使用AutomationElement的原因,我在这个小示例中尝试过:但没有成功,我得到了这个异常,我不确定其余的代码是否正常我在那里下载了源代码,并将其从“firefox”改为“chrome”。异常非常清楚,您传递的窗口句柄是
IntPtr.Zero
,它不是对窗口的有效引用。您始终可以使用工具-->Spy++获取窗口句柄并以这种方式进行测试。
        var cp = Process.GetProcessesByName("chrome");
        IntPtr ww = IntPtr.Zero;
        foreach (var p in cp) {
            if (p.MainWindowHandle != IntPtr.Zero) {
                ww = p.MainWindowHandle;
            }
        }