[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to auto populate a web page using Delphi

Posted on 2011-03-25
5
Medium Priority
?
471 Views
Last Modified: 2012-05-11
Hi, I wonder if anyone can help me I'm looking to post details to a web page and parse the results using Delphi 2010. For example I need Delphi to call https://www.mycarcheck.com/ and automatically populate the registation details and effectively click the "check" button.
Any help would be much appreciated.

Thanks  
0
Comment
Question by:Matt_Harding
[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
  • 3
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 2000 total points
ID: 35216284
Use TWebBrowser:
procedure TForm1.FormFill(WebBrowser1:TWebBrowser; ElementName, Value: string);
var
  i, j: Integer;
  FormItem: Variant;
begin

  for I := 0 to WebBrowser1.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser1.OleObject.Document.forms.Item(I);
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        //find the fieldname and fill out
        if FormItem.Item(j).Name = ElementName then
        begin
          FormItem.Item(j).Value := Value;
        end;
      except
        Exit;
      end;
    end;
  end;
end;

procedure TForm1.FormSubmit(WebBrowser1:TWebBrowser; ElementName, Value: string);
var
  i, j: Integer;
  FormItem: Variant;
begin

  for I := 0 to WebBrowser1.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser1.OleObject.Document.forms.Item(I);
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        //when the Submit Button is found click
        if FormItem.Item(j).type = ElementName then
        begin
          FormItem.Item(j).Value := Value;
          FormItem.Item(j).click;
        end;
      except
        Exit;
      end;
    end;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
WebBrowser1.Navigate('https://www.mycarcheck.com/');
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
FormFill(WebBrowser1, 'reg_no', 'xxxx');  // XXXX is the registration number
FormSubmit(WebBrowser1, 'submit', 'Check!'); // submit is the input type and Login text or value on the button.
end;

Open in new window

0
 

Author Comment

by:Matt_Harding
ID: 35216534
That's excellent thanks very much, I was messing around with Indy's TIdHTTP component without much joy.

I'd like to run this code without displaying the WebBrowser and by parsing the results in my code, can you tell me how I can access the html that's returned in the WebBrowser component after your FormSubmit is called?

Once again thanks for your assistance.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35216608
To get the HTML when the loading of the page is completed use:
uses mshtml;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
    iall : IHTMLElement;
begin
     iall := (WebBrowser1.Document AS IHTMLDocument2).body;

     while iall.parentElement <> nil do
     begin
       iall := iall.parentElement;
     end;
     memo1.Text := iall.outerHTML;
end;

Open in new window


If you want to get the HTML when a button is clicked then:
uses mshtml;

var
    iall : IHTMLElement;
begin
   if Assigned(WebBrowser1.Document) then
   begin
     iall := (WebBrowser1.Document AS IHTMLDocument2).body;

     while iall.parentElement <> nil do
     begin
       iall := iall.parentElement;
     end;
     memo1.Text := iall.outerHTML;
   end;
end;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35216652
Here is a complete approach:
uses mshtml;

var
  flag : boolean = False;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
    iall : IHTMLElement;
begin
  if flag then
    begin
     iall := (WebBrowser1.Document AS IHTMLDocument2).body;

     while iall.parentElement <> nil do
     begin
       iall := iall.parentElement;
     end;
     memo1.Text := iall.outerHTML;
    end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
flag := false;
WebBrowser1.Navigate('https://www.mycarcheck.com/');
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
FormFill(WebBrowser1, 'reg_no', 'xxxx');  // XXXX is the registration number
FormSubmit(WebBrowser1, 'submit', 'Check!'); // submit is the input type and Login text or value on the button.
flag := true;
end;

Open in new window

0
 

Author Comment

by:Matt_Harding
ID: 35216971
That's great, thanks again for all your help much appreciated.

Regs

Matt
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

649 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