?
Solved

Twebbrowser

Posted on 2004-03-27
7
Medium Priority
?
378 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
ID: 10695098
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
ID: 10695306
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
ID: 10695361
I assume you would have to know the exact id of the field in order to do that.

Shane
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jexd99
ID: 10695405
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 1000 total points
ID: 10695699
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
ID: 10695800
Thanks for the help.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10695915
your welcome!

Happy Coding!

Shane
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

840 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