Solved

Twebbrowser

Posted on 2004-03-27
7
348 Views
Last Modified: 2010-04-05
I have a Twebbrowser on my screen with a page loaded.  I want to fill in the form by clicking a button, I thought this would be rather simple but it appears to have stumped me :)   How do I send some values into the Twebbrowser edit boxes on the page that is currently loaded?... in other words I want to send 'Test 1' into the first edit box, then 'Test 2' into the 2nd edit box?  

        Thanks
0
Comment
Question by:jexd99
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
To test this code put a TWebBrowser and A TButton component on the form }


function FillForm(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean;  
var  
  i, j: Integer;  
  FormItem: Variant;  
begin  
  Result := False;  
  //no form on document  
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then  
  begin  
    Exit;  
  end;  
  //count forms on document  
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do  
  begin  
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);  
    for j := 0 to FormItem.Length - 1 do  
    begin  
      try  
        //when the fieldname is found, try to fill out  
        if FormItem.Item(j).Name = FieldName then  
        begin  
          FormItem.Item(j).Value := Value;  
          Result := True;  
        end;  
      except  
        Exit;  
      end;  
    end;  
  end;  
end;  


//When the document is complete try to fill out the field homepage with the url  
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;  
  const pDisp: IDispatch; var URL: OleVariant);  
begin  
  if FillForm(WebBrowser1, 'homepage', 'http://www.swissdelphicenter.ch') = False then  
    ShowMessage('Error. Field not available or no Form found.');
end;

// Show the Webbrowser-Progress in Label1  
procedure TForm1.WebBrowser1ProgressChange(Sender: TObject; Progress, ProgressMax: Integer);  
begin  
  if ProgressMax = 0 then  
  begin  
    label1.Caption := '';  
    Exit;  
  end;  
  try  
    if (Progress <> -1) and (Progress <= ProgressMax) then  
      label1.Caption := IntToStr((Progress * 100) div ProgressMax) + '% loaded...'  
    else  
      label1.Caption := '';  
  except  
    on EDivByZero do Exit;  
  end;  
end;  


//For example you can load the page /en/addtip.php to the TWebBrowser  
//When the document is Complete the form where you can put your homepage  
//address is filled out  
procedure TForm1.Button1Click(Sender: TObject);  
begin  
  Webbrowser1.Navigate('http://www.swissdelphicenter.ch/en/addtip.php');  
  // Show the Titel of the currently active Webpage in the titlebar  
  // Den Titel der aktuellen Webseite in der Titeleiste anzeigen  
  Caption := Webbrowser1.OleObject.Document.Title;  
end;
0
 

Author Comment

by:jexd99
Comment Utility
Works great, but I still have a small problem  :)   I want to put the cursor on an edit box and click the button.  For example this will fill in from top to bottom if you know the name of the field for the edit box, but if I want to put my cursor on the 8th edit box on the page, and click the button, it would then fill the edit box where my cursor is located (which could be anywhere or any edit box).   This example would do exactly that if I could identify where the cursor was sitting when I clicked the button.  Is there a way to identify this?   Thanks
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
I assume you would have to know the exact id of the field in order to do that.

Shane
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:jexd99
Comment Utility
Hmmm, I've seen other programs do this. Is there a way to identify the field that I'm currently in? ... if so I can use a timer to always have the last field name, then this would work.
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 250 total points
Comment Utility
something like

var
  aHTMLDoc : IHTMLDocument2;
  vWebForm : OleVariant;
  aEC      : IHTMLElementCollection;
  i        : Integer;
  vControl : OleVariant;

  if not VarIsEmpty(WebBrowser.OleObject.Document) then
  begin
    aHTMLDoc := WebBrowser1.Document as IHTMlDocument2;
   //get the form name here
    vWebForm := aHTMLDoc.Forms.Item('FormName', 0);
  //get all form elements
    aEC := IDispatch(vWebForm.all) as IHTMLElementCollection;
  //step through each element
   for i := 0 to aEC.Length - 1 do
    begin
      vControl := aEC.Item(i, 0);
      if not VarIsEmpty(vControl) and vControl.isTextEdit then //not sure if its textedit or something else here
      begin  
      end;
    end;
  end;


Anyway, im not 100% sure about what the form element names are, but your looking for a edit field and a button
maybe you can tweak this with what you know.

You may have to brush up on the DOM (document object model) and find out what they call everything

Shane
0
 

Author Comment

by:jexd99
Comment Utility
Thanks for the help.
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
your welcome!

Happy Coding!

Shane
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now