C#IE11自动化-无法连接到打开的IE窗口

C#IE11自动化-无法连接到打开的IE窗口,c#,automation,internet-explorer-11,C#,Automation,Internet Explorer 11,我正在尝试连接到已打开的Internet Explorer窗口。一旦连接,我需要发送一些按键(通过SendKeys)到IE窗口进行一些处理。下面的代码在SendKeys命令之前一直有效。它会找到标题为“图形数据库”的IE窗口。当它点击“SendKeys.Send(“{TAB}”);”时,我得到一个错误“发生了一个类型为“System.NullReferenceException”的未处理异常” 附加信息:我还获得了以下关于NullReferenceException错误的信息。奇怪的是,如果我编

我正在尝试连接到已打开的Internet Explorer窗口。一旦连接,我需要发送一些按键(通过SendKeys)到IE窗口进行一些处理。下面的代码在SendKeys命令之前一直有效。它会找到标题为“图形数据库”的IE窗口。当它点击“SendKeys.Send(“{TAB}”);”时,我得到一个错误“发生了一个类型为“System.NullReferenceException”的未处理异常”

附加信息:我还获得了以下关于NullReferenceException错误的信息。奇怪的是,如果我编写代码打开一个新的IE窗口,然后使用SendKeys,它就可以正常工作了。连接到现有windows可能会导致此问题

SendKeys无法在此应用程序内运行,因为该应用程序未处理Windows消息。更改应用程序以处理消息,或使用SendKeys.SendWait方法

谁能帮我想一想怎么解决这个问题

安迪


我能够解决这个问题。我永远无法让IE.Visible=真正投入工作。这在我的代码中似乎没有任何作用。我必须使用setForeGroundIndow()来设置IE窗口的焦点

// Find the IE window 
int hWnd = FindWindow(null, "Graphics Database - Internet Explorer"); 

if (hWnd > 0) // The IE window was found. 
{ 
    // Bring the IE window to the front. 
    SetForegroundWindow(hWnd);
这个网站极大地帮助了我使setForeGroundIndow()工作


安迪,请容忍我,因为这会很长时间。首先,您需要查看mshtml文档和Dom。我不知道为什么自动化如此复杂,但事实确实如此。UIautomation类对windows应用程序非常有用,但对于IE来说,我却找不到真正适用的。其他人将指向第三方,如waitn和selenium。Waitn似乎不再受支持,selenium也不允许您使用开放式IE浏览器。我最近走上了这条路,因为我想创建一个应用程序来存储我的网络密码并自动填写,因为由于安全限制,我无法在浏览器中保存我的用户名和密码。我这里有一个例子,希望能有所帮助。首先打开IE并导航到。然后创建一个引用了mshtml和shdocvw的控制台项目。下面是代码。它获取窗口,然后查找用户名、密码和提交的元素。然后填充用户名和密码并单击提交按钮。我没有登录到这个网站,所以它不会让你登录。我一直在用它进行测试。我遇到的问题是网站使用javascript登录表单。如果你进一步了解这些信息,请发回,因为我仍在努力发展这些概念,并创造一些可重用的东西

        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

        Console.WriteLine("Starting Search\n\n\n");

        foreach (SHDocVw.InternetExplorer ie in shellWindows)
        {
            if (ie.LocationURL.Contains("aavtrain"))
            {
                Console.WriteLine(ie.LocationURL);
                Console.WriteLine("\n\n\n\n");
                Console.WriteLine("FOUND!\n");

                mshtml.HTMLDocument document = ie.Document;
                mshtml.IHTMLElementCollection elCol = document.getElementsByName("user_name");
                mshtml.IHTMLElementCollection elCol2 = document.getElementsByName("password");
                mshtml.IHTMLElementCollection elCol3 = document.getElementsByName("Submit");

                Console.WriteLine("AutofillPassword");


                foreach (mshtml.IHTMLInputElement i in elCol)
                {
                    i.defaultValue = "John";
                }

                foreach (mshtml.IHTMLInputElement i in elCol2)
                {
                    i.defaultValue = "Password";
                }

                Console.WriteLine("Will Click Button in 2 seconds");
                Thread.Sleep(2000);

                foreach (mshtml.HTMLInputButtonElement i in elCol3)
                {

                    i.click();
                }


            }
        }

        Console.WriteLine("Finished");

堆栈跟踪或…我发现更多问题是IE.Visible=true;它不起作用。如果我在IE.Visible=true前后使用MessageBox暂停代码,并手动单击图形数据库窗口使其处于活动状态,则以下代码将按预期工作。我能够解决此问题。我永远无法让IE.Visible=真正投入工作。这在我的代码中似乎什么都没有。我必须使用setForeGroundIndow()来设置IE窗口的焦点。
        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();

        Console.WriteLine("Starting Search\n\n\n");

        foreach (SHDocVw.InternetExplorer ie in shellWindows)
        {
            if (ie.LocationURL.Contains("aavtrain"))
            {
                Console.WriteLine(ie.LocationURL);
                Console.WriteLine("\n\n\n\n");
                Console.WriteLine("FOUND!\n");

                mshtml.HTMLDocument document = ie.Document;
                mshtml.IHTMLElementCollection elCol = document.getElementsByName("user_name");
                mshtml.IHTMLElementCollection elCol2 = document.getElementsByName("password");
                mshtml.IHTMLElementCollection elCol3 = document.getElementsByName("Submit");

                Console.WriteLine("AutofillPassword");


                foreach (mshtml.IHTMLInputElement i in elCol)
                {
                    i.defaultValue = "John";
                }

                foreach (mshtml.IHTMLInputElement i in elCol2)
                {
                    i.defaultValue = "Password";
                }

                Console.WriteLine("Will Click Button in 2 seconds");
                Thread.Sleep(2000);

                foreach (mshtml.HTMLInputButtonElement i in elCol3)
                {

                    i.click();
                }


            }
        }

        Console.WriteLine("Finished");