简体   繁体   中英

How to automatically click a dialog box opened from console application

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM