0

我正在尝试自动化 ie。这是我捕获 ie 窗口的代码

ProcessStartInfo psi = new ProcessStartInfo();
            psi.CreateNoWindow = false;
            psi.FileName = "IExplore.exe";
            psi.Arguments = "-nomerge about:blank ";
            psi.WindowStyle = ProcessWindowStyle.Normal;
            Process p = new Process();
            p.StartInfo = psi;

            if (p.Start())
            {
                int maxWait = 10000, wait = 0;
                while (!p.HasExited && (p.MainWindowHandle == IntPtr.Zero))
                {
                    wait += 10;
                    Thread.Sleep(10);
                    p.Refresh();

                    if (wait > maxWait) break;
                }

                wait = 0;
                while (!p.HasExited && (_IE == null))
                {
                    _IE = null;
                    ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();//here i get exception
                    foreach (InternetExplorer Browser in m_IEFoundBrowsers)
                    {
                        if (Browser.HWND == (int)p.MainWindowHandle)
                        {
                            _IE = Browser;
                            break;
                        }
                    }

                    if ((_IE != null) || (wait > maxWait)) break;
                    else
                    {
                        wait += 10;
                        Thread.Sleep(10);
                    }
                }

                if (_IE != null)
                {
                    IE.Visible = true;
                    IE.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
                }
                else
                {
                    Console.WriteLine("Problem opening IE!");
                }
            }

这段代码正常工作,但是当我尝试通过 remoteapp 启动应用程序时,我得到异常,我猜原因是一些访问相关但不确定要做什么。请帮忙

4

1 回答 1

1

终于让它工作了,只需用小代码替换上面的大代码

**

_IE = new InternetExplorer();
                IE.Visible = true;
                IE.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
                var handle = GetConsoleWindow();
                ShowWindow(handle, SW_HIDE);

**

但是在这里,如果自动化失败并且卡住,那么我也会遇到异常,然后休息所有自动化将开始抛出这个异常。解决方案是我需要从任务管理器关闭失败的实例,然后一切都会再次正常工作。

于 2014-01-10T10:20:16.547 回答