How to automatically click a dialog box opened from console application

2.6k views Asked by At

I am using a Fiddler Core in which when I press "t" for Trust Root Certificate, it shows a DialogBox of "Security Warning" to press Yes or No.

I want to automate this section that when the dialog box opens my console application automatically clicks Yes.

1

There are 1 answers

0
Sadovyj Michael On

If you want to click on button in another window - at first find title and class name of window. This can be done with Spy++, which is in start menu folder (Microsoft Visual Studio 2010/Visual Studio Tools/Spy++). In spy++ just press Search/find window..., than point to desirable window.

And now you can send key 'enter' to YOUR window (or first send 'tab', if [no] button is active)

Here's a link How to: Simulate Mouse and Keyboard Events in Code

example (works great with my firefox) :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Temp
{
    class Program
    {
        static void Main(string[] args)
        {
            bool IsDone = false;

            while (IsDone == false)
            {
                // Get a handle to the Firefox application. The window class 
                // and window name were obtained using the Spy++ tool.
                IntPtr firefoxHandle = FindWindow("MozillaWindowClass", null);

                // Verify that Firefox is a running process. 
                if (firefoxHandle == IntPtr.Zero)
                {
                    // log of errors
                    Console.WriteLine("Firefox is not running.");

                    // wait 1 sec and retry
                    System.Threading.Thread.Sleep(1000);
                    continue;
                }

                // Make Firefox the foreground application and send it commands 
                SetForegroundWindow(firefoxHandle);

                // send keys to window
                System.Windows.Forms.SendKeys.SendWait("google.com");
                System.Windows.Forms.SendKeys.SendWait("{tab}");
                System.Windows.Forms.SendKeys.SendWait("{enter}");

                // yeah ! job's done
                IsDone = true;
            }
        }

        // Get a handle to an application window.
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
}