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
Solved

How to auto populate a web page using Delphi

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 381
Delphi 2 69
Performance of SQL statement 37 127
Installshield for Embarcadero EX 10.1 Berlin 4 60
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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