Simulate click on button of HTML-form in TWebBrowser

Posted by Johan 14205 on January 03, 2008

In Reply to Re: Webbrowser and modal dialog boxes posted by Hard Style on January 01, 2008

: 1. I believe that the dialog box is part of the web page confirmation of data change as it is certainly not part of my coding.
: 2. The dialog has only the two buttons YES or NO so a button click will be needed.

Look at the HTML code of the web page that displays the "confirmation dialog". You wrote that there are two buttons on it, so they probably are part of a web-"form". Somewhere between the web page HTML codes and there is something like this:

<input type="submit" name="..." value="Yes">
<input type="submit" name="..." value="No">

The text after VALUE= is what's displayed on top of the webpage-form-button. If there is only 1 webform on the page, and if there is only 1 button with the text "YES" (or "Yes" or "yes), your Delphi code has to find this element and simulate a click on it. An example of the source code:

uses  ... MSHTML;
procedure TForm1.ClickYes;
  Doc: IHTMLDocument2;
  WebForm: IHTMLFormElement;
  FormElements: OleVariant;
  i: integer;
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if Assigned(Doc) then begin
    WebForm := Doc.Forms.Item(0,'') as IHTMLFormElement;
    FormElements := WebForm.Elements;
    // Search for element with value equal to "Yes" (or "YES" or "yes") 
    for i := 0 to FormElements.Length - 1 do begin
      if (Uppercase(FormElements.Item(i).Value) = 'YES') then begin
        // click on that element
    ShowMessage('Error: no document loaded');

Please let me know your results, if not OK then I can try to improve it :)

Good luck!

Related articles


Follow Ups