Solved

How to auto populate a web page using Delphi

Posted on 2011-03-25
5
432 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

12 Experts available now in Live!

Get 1:1 Help Now