Solved

Twebbrowser

Posted on 2004-03-27
7
356 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 250 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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

9 Experts available now in Live!

Get 1:1 Help Now