0

I am trying to automate ie.This is my code to catch ie window

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!");
                }
            }

This code works fine normally but when i try to launch application via remoteapp then i get exception i guess reason is some access related but nt sure wht to do. please help

ansh shah
  • 31
  • 5
  • Do you have more details of the exception you get? Please edit the post and include them. – Andrew Sep 19 '13 at 09:46
  • No this is the only error i get.Little experimenting i found out tht usually this error come when the program dnt find any browser open.So my assumption are tht due to access rights problem during remoteapp it is not able to catch ie window – ansh shah Sep 19 '13 at 10:25
  • I am running application on windows server 2008 r2. I have checked all hardening and group policy all is fine – ansh shah Sep 19 '13 at 10:27

1 Answers1

1

Finally got it working just replace above big code with small one

**

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

**

But here also i get above exception if automation failed and ie gets stuck then rest all automation will start throwing this exception. The resolution to that is i need to close the instance of failed ie from taskmanager then all will work fine again.

ansh shah
  • 31
  • 5