Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Twebbrowser

Posted on 2004-03-27
7
Medium Priority
?
375 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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