Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2011.01.02;
Скачать: CL | DM;

Вниз

Заполнить формы в браузере где есть несколько фреймов   Найти похожие ветки 

 
Антон Т.   (2010-10-05 23:35) [0]

Есть код (указан ниже), который заполняет форму в браузере. Но этот код не работает если на странице есть фреймы. А на странице собственно и есть два фрейма. Подскажите пожалуйста как изменить код, чтобы можно было заполнить данные фрейма.

function FillForm(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean;
 var
   i, j: Integer;
   FormItem: Variant;
 begin
   Result := False;
   //no form on document  
 if WebBrowser.OleObject.Document.all.tags("FORM").Length = 0 then
   begin
     Exit;
   end;
   //count forms on document
 for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do
   begin
     FormItem := WebBrowser.OleObject.Document.forms.Item(I);
     for j := 0 to FormItem.Length - 1 do
     begin
       try
         //when the fieldname is found, try to fill out
       if FormItem.Item(j).Name = FieldName then
         begin
           FormItem.Item(j).Value := Value;
           Result := True;
         end;
       except
         Exit;
       end;
     end;
   end;
    end;

Заранее спасибо.


 
Palladin ©   (2010-10-06 09:00) [1]


>  WebBrowser.OleObject.Document.all.tags("FORM")

а что означает эта строчка?


 
Антон Т.   (2010-10-06 11:02) [2]


> >  WebBrowser.OleObject.Document.all.tags("FORM")

Я вообще не профи и занимаюсь дельфи исключительно для души, для упрощения некоторых задач. Я так понимаю этот код проверяет есть ли вообще формы на странице... если их нет, то функция прерывается. Разве этот код имеет отношение к фреймам?


 
Palladin ©   (2010-10-06 11:38) [3]

только за деньги


 
stas ©   (2010-10-06 12:14) [4]

Я вот писал для себя разберись может что выбросить и можно использовать.


type TFormElement = class(TObject)
public
 Items:TStringList;
 ElementType:integer;
 Elementid:String;
 ElementName:String;
 ElementValue:String;
 ElementRealName:string;
 FrameIndex:Integer;
 Element:IHTMLElement;
//  ElementSrc:String;
Constructor Create;
Destructor Destroy;
end;
....

{ TFormElement }

constructor TFormElement.Create;
begin
inherited;
Items:=TStringList.Create;
end;

destructor TFormElement.Destroy;
begin
Items.Free;
inherited;
end;

procedure GetAllElemnts (Document:IHTMLDocument2;List:TStrings;FrameIndex:Integer=-1;Images:boolean=false);

var
 index: integer;
 field: IHTMLElement;
 input: IHTMLInputElement;
 select: IHTMLSelectElement;
 text: IHTMLTextAreaElement;
 ftype:Integer;
 F:TFormElement;
 i:Integer;
 name:String;
 frame:IDispatch;
 frame_win :IHTMLWindow2;
 frame_doc: IHTMLDocument2;
 j:OleVariant;
begin

 for index := 0 to document.all.length-1 do
 begin
   field := document.all.Item(index,"") as IHTMLElement;
  // if Assigned(field) then
   begin

   if not images then
    begin

     if field.tagName = "INPUT" then
     begin
       // ïîëÿ Input
       input := field as IHTMLInputElement;
       if input.type_ ="radio" then ftype:=3 else
       if input.type_ = "checkbox" then ftype:=2 else
       if (input.type_ ="submit") then  ftype:=5 else
       if (input.type_ ="button") then ftype:=0 else
       if input.type_ = "file" then ftype:=6 else
       ftype:=1;

       if (input.type_ <>"hidden") then
       begin
        if (input.name="") or (ftype=6) then name:=field.id else name:=Input.name;
        if name="" then name:=Input.value;
        f:=TFormElement.Create;
        f.ElementType:=ftype;
        f.Elementid:=Field.id;
        f.ElementValue:=Input.value;
        f.ElementName:=name;
        f.FrameIndex:=FrameIndex;
        f.ElementRealName:=Input.name;
        f.Element:=field;
        List.AddObject(name+"="+Input.value,f);
       end;
     end
     else if field.tagName = "SELECT" then
     begin
       // &#239;&#238;&#235;&#255; Select
        select := field as IHTMLSelectElement;
        if select.name="" then name:=field.id else name:=select.name;
         if name="" then name:=field.innerText;
        f:=TFormElement.Create;
        f.ElementType:=4;
        f.Elementid:=Field.id;
        f.ElementValue:=Select.value;
        f.ElementName:=name;
        f.FrameIndex:=FrameIndex;
        f.Element:=field;
        f.ElementRealName:=select.name;
       for i:=0 to select.length-1 do
        f.Items.Add((select.item(i,0) as IHTMLElement).innerText);
        List.AddObject (name+"="+Select.value,f);
     end
     else if field.tagName = "TEXTAREA" then
     begin
       // &#239;&#238;&#235;&#255; TextArea
        text := field as IHTMLTextAreaElement;
        if text.name="" then name:=field.id else name:=text.name;
        if name="" then name:=field.innerText;
        f:=TFormElement.Create;
        f.ElementType:=1;
        f.Elementid:=Field.id;
        f.ElementValue:=Text.value;
        f.ElementName:=name;
        f.FrameIndex:=FrameIndex;
        f.ElementRealName:=text.name;
        f.Element:=field;
        List.AddObject(text.name+"="+Text.value,f);
     end else
     if field.tagName = "BUTTON" then
      begin
        name:=field.id;
        if name="" then name:=field.innerText;
        f:=TFormElement.Create;
        f.ElementType:=0;
        f.Elementid:=Field.id;
        f.ElementValue:=Field.innerText;
        f.ElementName:=name;
        f.FrameIndex:=FrameIndex;
        f.Element:=field;
        f.ElementRealName:=name;
        List.AddObject(Field.id+"="+Field.innerText,f);
      end;
      end else
      if field.tagName = "IMG" then
      begin
        name:= (field as IHTMLImgElement).name;
       if name=""  then name:=field.id;
       if name="" then name:="Number"+IntToStr(List.Count);
        f:=TFormElement.Create;
        f.ElementType:=8;
        f.Elementid:=Field.id;
        f.ElementValue:=(field as IHTMLImgElement).src;
        f.ElementName:=name;
        f.FrameIndex:=FrameIndex;
        f.Element:=field;
        f.ElementRealName:=name;
        List.AddObject(name+"="+(field as IHTMLImgElement).src,f);
      end;

   end;

 end;

    for i:=0 to document.frames.length-1 do
     begin
      J:=OleVariant(i);
      try
       frame:= document.frames.item(j);
       frame_win := frame as IHTMLWindow2;
       frame_doc := frame_win.document;
       GetAllElemnts (frame_doc,List,i,images);
      except
      end;
     end;
end;


 
stas ©   (2010-10-06 12:17) [5]

Это получишь список всех элементов в TStringList, а как обратится потом к каждому элементу и что-то присвоить думаю понятно.


 
Антон Т.   (2010-10-06 14:25) [6]

Ответ уважаемого stas оставил не однозначное впечатление. С одной стороны, я рад что остались хорошие люди готовые помочь, с другой стороны, я опечален, что не понимаю как модифицировать его код под свою задачу. Уважаемый stas, нельзя ли попросить изменить тот код, что указал я... в вашем я честно говоря не разобрался.


 
stas ©   (2010-10-06 16:36) [7]

У Вас получилось наполнить TStringList  элементами?


 
Антон Т.   (2010-10-06 22:13) [8]

2 stas
спасибо, разобрался!



Страницы: 1 вся ветка

Текущий архив: 2011.01.02;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.008 c
15-1284046698
@!!ex
2010-09-09 19:38
2011.01.02
Фриланс заказ:3 функции для работы с географическими координатами


2-1286286239
alekslep
2010-10-05 17:43
2011.01.02
Delphi и 2 бд


2-1286814759
M4
2010-10-11 20:32
2011.01.02
Вопрос???


2-1282054512
Black123
2010-08-17 18:15
2011.01.02
TWebBrowser и WEB2.0


2-1286534138
12
2010-10-08 14:35
2011.01.02
MSSQL. Два столбца в одну строку.