Solved

TWebBrowser automation problem - code included.

Posted on 2007-04-11
4
506 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
[X]
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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

617 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