Solved

How to auto populate a web page using Delphi

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

11 Experts available now in Live!

Get 1:1 Help Now