Solved

TWebBrowser automation problem - code included.

Posted on 2007-04-11
4
491 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

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 287
Need Delphi function to get Youtube video title 5 227
How to fill array with TArray.Create? 14 79
select query - oracle 16 95
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 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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

14 Experts available now in Live!

Get 1:1 Help Now