Solved

TWebBrowser automation problem - code included.

Posted on 2007-04-11
4
487 Views
Last Modified: 2008-03-18
Hi.
Got some code for automated TWebBrowser forms filling, but in some cases it isnt working... any idea why? Getting this error: "method 'name' not supported by automation object" ... and it DOES work in almost every site... but for example not here: http://profil.interia.pl/nowe_konto/

It halts on "WbSetEditField(webbrowser1,'formLogin',login);" for example...

<code>
unit WbCtrl;       

interface       

uses Windows, Classes, Forms, ShDocVw_TLB, WinInet, ActiveX;       

function WbEnumFields(WebBrowser: TWebBrowser; SL: TStrings): Boolean;       
function WbSetEditField(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean;       
function WbGetEditField(WebBrowser: TWebBrowser; FieldName: string; var Value: string): Boolean;       
function WbSetCheckBoxField(WebBrowser: TWebBrowser; FieldName: string; Checked: boolean): Boolean;       
function WbGetCheckBoxField(WebBrowser: TWebBrowser; FieldName: string; var Checked: boolean): Boolean;       
function WbBtnClick(WebBrowser: TWebBrowser; FieldName: string): Boolean;
function WbBtnClickByValue(WebBrowser: TWebBrowser; Value: string): Boolean;
function WbBtnSubmitByWidth(WebBrowser: TWebBrowser; ButtonWidth: string): Boolean;
function WbSetEditFieldByNameAndSize(WebBrowser: TWebBrowser; FieldName: string; Size: string; Value: string): Boolean;
function WbBtnClickByValueAndName(WebBrowser: TWebBrowser; Value: string; FName: string): Boolean;

implementation       

// Lese alle Feldnamen aus       
// Übergabe:       
// Webbroweser: Webbrowser, der die aktuelle Seite enthält       
// Sl         : Die Stringlist zu der alle Feldnamen hinzugefügt werden sollen       
// Result     : true  -> Feld existiert       
//            : false -> Feld existiert nicht       
//                       oder kein Dokument geladen       
function WbEnumFields(WebBrowser: TWebBrowser; SL: TStrings): Boolean;       
var       
  i, j: Integer;       
  FormItem: Variant;       
begin       
  Result := True;       
  //Hat das Dokument Forms? (Frames)       
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then       
  begin       
    Result := False;       
    Exit;       
  end;       
  //Bearbeite alle Frames       
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do       
  begin       
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);       
    // Bearbeite alle Felder eines Frames       
    for j := 0 to FormItem.Length - 1 do       
    begin       
      try       
        SL.Add(FormItem.Item(j).Name);       
      except       
        Result := False;       
        Exit;       
      end;       
    end;       
  end;       
end;       

// Durchsuche alle Formulare im Webbrowser nach dem Feldnamen       
// Übergabe:       
// Webbroweser: Webbrowser, der die aktuelle Seite enthält
// FieldName  : Name des gesuchten Felds
// Rückgabe
// Field      : Field.x : Frame Nummer
//            : Fielf.y : Feld Nummer im Frame
// Result     : true  -> Feld existiert
//            : false -> Feld existiert nicht
//                       oder kein Dokument geladen
function WbFindField(WebBrowser: TWebBrowser; FieldName: string; var Field: TPoint): boolean;
var
  i, j: Integer;       
  FormItem: Variant;
begin       
  Result := False;
  //Hat das Dokument Forms? (Frames)
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
  begin
    Exit;
  end;
  //Bearbeite alle Frames
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);
    // Bearbeite alle Felder eines Frames
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        // Feldname wurde gefunden
        // gebe ihn zurück und ende
        if FormItem.Item(j).Name = FieldName then
        begin
          Field.x := i;          // Position speichern
          Field.Y := j;
          Result := True;
          exit;
        end;
      except
        Exit;
      end;
    end;
  end;
end;

function WbFindFieldByNameAndSize(WebBrowser: TWebBrowser; FieldName: string; Size: string; var Field: TPoint): boolean;
var
  i, j: Integer;       
  FormItem: Variant;
begin       
  Result := False;
  //Hat das Dokument Forms? (Frames)
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
  begin
    Exit;
  end;
  //Bearbeite alle Frames
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);
    // Bearbeite alle Felder eines Frames
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        // Feldname wurde gefunden
        // gebe ihn zurück und ende
        if (FormItem.Item(j).Name = FieldName) and (FormItem.Item(j).Size = Size) then
        begin
          Field.x := i;          // Position speichern
          Field.Y := j;
          Result := True;
          exit;
        end;
      except
        Exit;
      end;
    end;
  end;
end;

function WbFindFieldByValue(WebBrowser: TWebBrowser; Value: string; var Field: TPoint): boolean;
var
  i, j: Integer;
  FormItem: Variant;
begin
  Result := False;
  //Hat das Dokument Forms? (Frames)
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
  begin
    Exit;
  end;
  //Bearbeite alle Frames
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);
    // Bearbeite alle Felder eines Frames
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        // Feldname wurde gefunden
        // gebe ihn zurück und ende
        if pos(Value,FormItem.Item(j).Value) > 0 then
        begin
          Field.x := i;          // Position speichern
          Field.Y := j;
          Result := True;
          exit;       
        end;
      except       
        Exit;
      end;
    end;
  end;
end;

function WbFindFieldByValueAndName(WebBrowser: TWebBrowser; Value: string; FName:string; var Field: TPoint): boolean;
var
  i, j: Integer;
  FormItem: Variant;
begin
  Result := False;
  //Hat das Dokument Forms? (Frames)
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then
  begin
    Exit;
  end;
  //Bearbeite alle Frames
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
  begin
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);
    // Bearbeite alle Felder eines Frames
    for j := 0 to FormItem.Length - 1 do
    begin
      try
        // Feldname wurde gefunden
        // gebe ihn zurück und ende
        if (pos(Value,FormItem.Item(j).Value) > 0) and (FormItem.Item(j).Name = FName) then
        begin
          Field.x := i;          // Position speichern
          Field.Y := j;
          Result := True;
          exit;       
        end;
      except       
        Exit;
      end;
    end;
  end;
end;

function WbFindSubmitByWidth(WebBrowser: TWebBrowser; ButtonWidth: string; var Field: TPoint): boolean;
var       
  i, j: Integer;       
  FormItem: Variant;       
begin       
  Result := False;       
  //Hat das Dokument Forms? (Frames)       
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then       
  begin       
    Exit;       
  end;       
  //Bearbeite alle Frames       
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do       
  begin       
    FormItem := WebBrowser.OleObject.Document.forms.Item(I);       
    // Bearbeite alle Felder eines Frames       
    for j := 0 to FormItem.Length - 1 do       
    begin       
      try       
        // Feldname wurde gefunden       
        // gebe ihn zurück und ende       
        if (FormItem.Item(j).type = 'submit') and (FormItem.Item(j).width = ButtonWidth) then
        begin       
          Field.x := i;          // Position speichern       
          Field.Y := j;       
          Result := True;       
          exit;       
        end;       
      except       
        Exit;       
      end;       
    end;       
  end;       
end;

// Trage Text in ein Editfeld ein       
// Übergabe:       
// Webbroweser: Webbrowser, der die aktuelle Seite enthält       
// FieldName  : Name des gesuchten Felds       
// Rückgabe       
// Value      : Der Text, der in das Feld hinein soll       
// Result     : true  -> kein Fehler       
//            : false -> Feld existiert nicht       
//                       oder kein Dokument geladen       
function WbSetEditField(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean;       
var       
  Field: TPoint;       
begin       
  // Position des Felds holen       
  if not WbFindField(WebBrowser, FieldName, Field) then       
  begin       
    Result := false;  // Feld existiert nicht       
    exit;             // Ende       
  end;       
  try       
    // Feld füllen       
    WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Value := Value;       
    Result := True;       
  except       
    Result := false;       
    Exit;       
  end;       
end;

function WbSetEditFieldByNameAndSize(WebBrowser: TWebBrowser; FieldName: string; Size: string; Value: string): Boolean;
var       
  Field: TPoint;       
begin       
  // Position des Felds holen       
  if not WbFindFieldByNameAndSize(WebBrowser, FieldName, Size, Field) then
  begin       
    Result := false;  // Feld existiert nicht       
    exit;             // Ende       
  end;       
  try
    // Feld füllen       
    WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Value := Value;       
    Result := True;       
  except       
    Result := false;       
    Exit;       
  end;       
end;

// Lese Text aus einem Editfeld       
// Übergabe:       
// Webbroweser: Webbrowser, der die aktuelle Seite enthält       
// FieldName  : Name des gesuchten Felds       
// Rückgabe       
// Value      : Text, der im Feld steht       
// Result     : true  -> kein Fehler       
//            : false -> Feld existiert nicht       
//                       oder kein Dokument geladen       
function WbGetEditField(WebBrowser: TWebBrowser; FieldName: string; var Value: string): Boolean;       
var       
  Field: TPoint;       
begin       
  // Position des Felds holen       
  if not WbFindField(WebBrowser, FieldName, Field) then       
  begin       
    Result := false;  // Feld existiert nicht       
    exit;             // Ende       
  end;       
  try       
    // Feld lesen       
    Value := WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Value;       
    Result := True;       
  except       
    Result := false;       
    Exit;       
  end;       
end;       

// Setze eine Checkbox       
// Übergabe:       
// Webbroweser: Webbrowser, der die aktuelle Seite enthält       
// FieldName  : Name des gesuchten Felds       
// Rückgabe       
// Checked    : Zustand       
// Result     : true  -> kein Fehler       
//            : false -> Feld existiert nicht       
//                       oder kein Dokument geladen       
function WbSetCheckBoxField(WebBrowser: TWebBrowser; FieldName: string; Checked: boolean): Boolean;       
var       
  Field: TPoint;       
begin       
  // Position des Felds holen       
  if not WbFindField(WebBrowser, FieldName, Field) then       
  begin       
    Result := false;  // Feld existiert nicht       
    exit;             // Ende       
  end;       
  try       
    // CheckBox setzen       
    WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Checked := Checked;       
    Result := True;       
  except       
    Result := false;       
    Exit;       
  end;       
end;       

// Lese eine Checkbox       
// Übergabe:       
// Webbroweser: Webbrowser, der die aktuelle Seite enthält       
// FieldName  : Name des gesuchten Felds       
// Rückgabe       
// Checked    : Zustand       
// Result     : true  -> kein Fehler       
//            : false -> Feld existiert nicht       
//                       oder kein Dokument geladen       
function WbGetCheckBoxField(WebBrowser: TWebBrowser; FieldName: string; var Checked: boolean): Boolean;       
var       
  Field: TPoint;       
begin       
  // Position des Felds holen       
  if not WbFindField(WebBrowser, FieldName, Field) then       
  begin       
    Result := false;  // Feld existiert nicht       
    exit;             // Ende       
  end;       
  try       
    // CheckBox setzen       
    Checked := WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Checked;       
    Result := True;       
  except       
    Result := false;       
    Exit;       
  end;       
end;       

// Click einen Button       
// Übergabe:       
// Webbroweser: Webbrowser, der die aktuelle Seite enthält       
// FieldName  : Name des Button       
// Rückgabe       
// Result     : true  -> kein Fehler       
//            : false -> Feld existiert nicht       
//                       oder kein Dokument geladen       
function WbBtnClick(WebBrowser: TWebBrowser; FieldName: string): Boolean;
var
  Field: TPoint;
begin
  // Position des Felds holen
  if not WbFindField(WebBrowser, FieldName, Field) then
  begin
    Result := false;  // Feld existiert nicht
    exit;             // Ende
  end;
  try       
    // CheckBox setzen
    WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Click;
    Result := True;       
  except
    Result := false;       
    Exit;
  end;
end;

function WbBtnClickByValue(WebBrowser: TWebBrowser; Value: string): Boolean;
var
  Field: TPoint;
begin
  // Position des Felds holen
  if not WbFindFieldByValue(WebBrowser, Value, Field) then
  begin
    Result := false;  // Feld existiert nicht
    exit;             // Ende
  end;
  try
    // CheckBox setzen
    WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Click;
    Result := True;
  except
    Result := false;
    Exit;
  end;
end;

function WbBtnClickByValueAndName(WebBrowser: TWebBrowser; Value: string; FName: string): Boolean;
var
  Field: TPoint;
begin
  // Position des Felds holen
  if not WbFindFieldByValueAndName(WebBrowser, Value, FName, Field) then
  begin
    Result := false;  // Feld existiert nicht
    exit;             // Ende
  end;
  try
    // CheckBox setzen
    WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Click;
    Result := True;
  except
    Result := false;
    Exit;
  end;
end;

function WbBtnSubmitByWidth(WebBrowser: TWebBrowser; ButtonWidth: string): Boolean;
var
  Field: TPoint;
begin
  // Position des Felds holen
  if not WbFindSubmitByWidth(WebBrowser, ButtonWidth, Field) then
  begin
    Result := false;  // Feld existiert nicht
    exit;             // Ende
  end;
  try
    // CheckBox setzen
    WebBrowser.OleObject.Document.forms.Item(Field.x).Item(Field.y).Click;
    Result := True;
  except
    Result := false;
    Exit;
  end;
end;



end.
</code>
0
Comment
Question by:flasht
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 18898912
Using the element's "location" on the web form is problematic.
It is best to just use the element's name or id. Use the technique
I outline here and you will be more successful:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21814080.html
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 19920877
flasht,
  What is the status of this question? Did you try my suggestion? Do you need more help?
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21156547
Forced accept.

Computer101
EE Admin
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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