title: C# clicking a javascript alert message OK button in webbrowser control
body: <p>I am using a webbrowser control in a C# windowsform program that navigates through several pages of a website and then uses some of the forms in the website to conduct a transaction.  (I tried to do this with <code>httpwebrequest</code> and <code>webclient</code>, but ran into difficulties with cookies and replicating some of how the website dynamically generates some of the form selection choices. I decided to use the webbrowser and take advantage of the website's scripting - it is not my site).</p>
  
  <p>In one of the final steps I reach a page with a form where the site runs a validation script on a page when the form is submitted.  If the user has entered incorrect info an alert pops up.</p>
  
  <p>But when I navigate to that page in my program (before I give values to the empty fields), I get the alert.  This doesn't happen when I do it manually with Chrome, Firefox or IE.  But it happens in the webbrowser.  I can replicate this in a regular browser by submitting the form with info that doesn't validate - but in web browser it happens when I load the page.</p>
  
  <p>My goal is to:</p>
  
  <ol>
  <li><p>detect that the popup alert has appeared and taken focus. (The name of the alert is "Message from webpage.")</p></li>
  <li><p>click the OK button of the alert, and let my program proceed to enter the information and continue through the end of the process.</p></li>
  </ol>
  
  <p>There are several questions similar to mine here. The most promising post i found has the following code:</p>
  
  <pre><code>[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);
      }
  </code></pre>
  
  <p>This code is a little beyond my newbie understanding - I tried setting it up in a new class and then instantiating an object of this class I navigate to the page in question and then invoking the <code>ClickOKButton</code> method. Didn't work.  I also tried including it at the form level and then running the <code>ClickOKButton</code> function at the point in the program where I navigate to the page where the alert appears.  But it is not working.</p>
  
  <p>So I have a few questions:</p>
  
  <ol>
  <li><p>is there another way to address the alert popup?</p></li>
  <li><p>assuming that this code makes sense, what kind of conditional test can I run before invoking this code (how can I check that the alert has appeared?)</p></li>
  <li><p>The page loads after an <code>InvokeMember("submit")</code> command of the previous page's form, and that is when the alert appears.  The next step in my code after the submit is a documentcompleted event handler which fires then completes the new form.  It is almost as if the webbrowser is submitting the form before I complete the fields.  Because of this I have no idea where to insert this <code>ClickOKButton</code> code.</p></li>
  <li><p>Of the things I do not understand in the code I found, I don't understand the "#32770" parameter that is being passed to FindWindow.  How can I know if that is right for my alert?</p></li>
  </ol>