In my C# Windows Forms program, I am utilizing a webbrowser control to navigate multiple pages of a website and interact with forms to carry out transactions. I initially tried using httpwebrequest and webclient, but encountered challenges with cookies and replicating the dynamic form generation on the website. Therefore, I opted for the webbrowser control to leverage the website's scripting capabilities (which is not under my ownership).
During one of the final steps, I encounter a page with a form where the site runs a validation script upon form submission. If incorrect information is inputted, an alert pops up.
The issue arises when I reach this page in my program - even before entering values into the fields, the alert triggers. This behavior does not occur when manually using Chrome, Firefox, or IE, but only within the webbrowser control. The alert appears as soon as the page loads, without submitting any data.
My objective is to:
Detect the appearance of the popup alert and bring it into focus (the alert is named "Message from webpage").
Click the OK button on the alert to allow my program to proceed with entering information and completing the transaction process.
I have come across similar questions, with one post providing code that seems promising:
[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 ClickOKButton()
{
IntPtr hwnd = FindWindow("#32770", "Message from webpage");
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
uint message = 0xf5;
SendMessage(hwnd, message, IntPtr.Zero, IntPtr.Zero);
}
I attempted to implement this code by creating a new class and invoking the ClickOKButton
method after reaching the problematic page, but it did not yield the desired results. I also tried integrating the code at the form level and executing the function at the point where the alert appears, yet it still did not work.
Therefore, I have several unanswered queries:
Is there an alternative approach to handling the alert popup?
If assuming the provided code is correct, what conditional test can be employed to verify if the alert has been triggered before executing the code?
Following the submit action of the previous page's form using
InvokeMember("submit")
, the subsequent page loads where the alert surfaces. My code includes a documentcompleted event handler which finalizes the new form after submission. It appears that the webbrowser submits the form prematurely, before filling out the fields, leaving me unsure of where to insert theClickOKButton
code.Regarding the unfamiliar aspects of the code snippet found, the "#32770" parameter passed to FindWindow puzzles me. How can I determine if this is appropriate for identifying my specific alert?