Solved

How to auto populate a web page using Delphi

Posted on 2011-03-25
5
438 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
  • 3
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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