Closing an Windows Dialogbox in Selenium C#

Let us say you are testing Facebook and you are clicking on the photo icon and you want to close the Window dialogbox that happens

enter image description here

For the issue like that use the below code to close the dialog box

 

//Code closes the Windows dialog    

[DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


    private static void CloseDialog()
    {
        var handle = FindWindow(null, "Give your window caption/title here");
        SetForegroundWindow(handle);
//send alt+f4 using sendkeys method
        System.Windows.Forms.SendKeys.SendWait("%{F4}");
    }

 

 

You may also like...