?
Solved

Twebbrowser

Posted on 2004-03-27
7
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 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