To programmatically click a warning window, we can use the Windows API FindWindow, FindWindowEx and SendMessage, the idea is to display a warning window, the form in which the WebBrowser control is placed will lose its concentration, at this time we can use FindWindow to get a window window handle warning, finally, we could send a click message to the "OK" button to click it.
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); private void Form1_Deactivate(object sender, EventArgs e) { IntPtr hwnd = FindWindow(null, "Message from webpage"); hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK"); uint message = 0xf5; SendMessage(hwnd, message, IntPtr.Zero, IntPtr.Zero); }
source share