GetFormFieldNames并不总是有效

时间:2011-03-11 11:02:33

标签: delphi browser ihtmldocument2

我试图找出哪种形式和元素也属于。我现在从这个网站了解的代码:

http://www.cryer.co.uk/brian/delphi/twebbrowser/read_write_form_elements.htm

包含此代码

function GetFormFieldNames(fromForm: IHTMLFormElement): TStringList;
var
  index: integer;
  field: IHTMLElement;
  input: IHTMLInputElement;
  select: IHTMLSelectElement;
  text: IHTMLTextAreaElement;
begin
  result := TStringList.Create;
  for index := 0 to fromForm.length do
  begin
    field := fromForm.Item(index,'') as IHTMLElement;
    if Assigned(field) then
    begin
      if field.tagName = 'INPUT' then
      begin
        // Input field.
        input := field as IHTMLInputElement;
        result.Add(input.name);
      end
      else if field.tagName = 'SELECT' then
      begin
        // Select field.
        select := field as IHTMLSelectElement;
        result.Add(select.name);
      end
      else if field.tagName = 'TEXTAREA' then
      begin
        // TextArea field.
        text := field as IHTMLTextAreaElement;
        result.Add(text.name);
      end;
    end;
  end;

end;

似乎适用于大多数网站。但是有一些网站,比如这个:

http://service.mail.com/registration.html#.1258-bluestripe-product1-undef

通过查看该代码并将其与活动ID进行比较,我可以找到它所在的表单。但是它不适用于该网站。由于某种原因,我认为它与htmldocument3和这个代码用于htmldocument2有关。但我不确定。

所以我的问题是如何从这个网站中提取包含所有元素名称的tstringlist?希望你能帮忙!

编辑:添加了一些代码

              begin

                theForm := GetFormByNumber(webbrowser1.document as IHTMLDocument2,
                  0);
                fields := GetFormFieldNames(theForm);
                num := fields.IndexOf(theid);
              end;
              until (num <> -1);

2 个答案:

答案 0 :(得分:1)

在网页中定位表单元素的一个复杂因素是页面可能包含框架,并且任何框架中都可能有表单。基本上,您必须遍历每个帧中的所有帧和表单。一旦您将表单作为IHTMLFormElement获取,请使用Cryer的函数来获取表单元素名称。

您提供的示例链接没有任何框架,您应该在获取表单元素列表时没有问题,除非您尝试按名称获取表单,因为它没有指定名称。使用以下过程

获取表单元素名称和值没有问题
procedure GetForms(doc1: IHTMLDocument2; var sl: TStringList);
var
  i, j, n: integer;
  docForm: IHTMLFormElement;
  slt:  TStringList;
  s: string;
begin
  if doc1 = nil then
  begin
    ShowMessage('doc1 is empty [GetForms]');
    Exit;
  end;
  slt := TStringList.Create;

  n := NumberOfForms(doc1);
  sl.Add('Forms: ' + IntToStr(n));
  for i := 0 to n - 1 do
  begin
    docForm := GetFormByNumber(doc1, i);
    sl.Add('Form Name: ' + docForm.Name);
    slt.Clear;
    slt := GetFormFieldNames(docForm);
    for j := 0 to slt.Count - 1 do
    begin
      s := GetFieldValue(docForm, slt[j]);
      sl.Add('Field Name: ' + slt[j] + '  value: "' + s + '"');
    end;
  end;
  sl.Add('');
  slt.Free;
end;

Cryer的navigating a frameset示例不适用于所有网站,请参阅http://support.microsoft.com/support/kb/articles/Q196/3/40.ASP。以下函数在我尝试过的所有网站上成功地将框架提取为IHTMLDocument2

function GetFrameByNumber(Doc:IHTMLDocument2; n:integer):IHTMLDocument2;
var
  Container: IOleContainer;
  Enumerator: ActiveX.IEnumUnknown;
  Unknown: IUnknown;
  Browser: IWebBrowser2;
  Fetched: Longint;
  NewDoc: IHTMLDocument2;
  i : integer;
begin
  // We cannot use the document's frames collection here, because
  // it does not work in every case (i.e. Documents from a foreign domain).
  // From: http://support.microsoft.com/support/kb/articles/Q196/3/40.ASP
  i := 0;
  if (Supports(Doc, IOleContainer, Container)) and
     (Container.EnumObjects(OLECONTF_EMBEDDINGS, Enumerator) = S_OK) then
  begin
    while Enumerator.Next(1, Unknown, @Fetched) = S_OK do
    begin
      if (Supports(Unknown, IWebBrowser2, Browser)) and
         (Supports(Browser.Document, IHTMLDocument2, NewDoc)) then
      begin
        // Here, NewDoc is an IHTMLDocument2 that you can query for
        // all the links, text edits, etc.
        if i=n then
        begin
          Result := NewDoc;
          Exit;
        end;
        i := i+1;
      end;
    end;
  end;
end;

以下是我如何使用GetForms和GetFrameByNumber

的示例
// from the TForm1 declaration
    { Public declarations }
    wdoc: IHTMLDocument2;


procedure TForm1.btnAnalyzeClick(Sender: TObject);
begin
  wdoc := WebBrowser.Document as IHTMLDocument2;
  GetDoc(wdoc);
end;

procedure TForm1.GetDoc(doc1: IHTMLDocument2);
var
  i, n: integer;
  doc2: IHTMLDocument2;
  frame_dispatch: IDispatch;
  frame_win: IHTMLWindow2;
  ole_index: olevariant;
  sl: TStringList;
begin
  if doc1 = nil then
  begin
    ShowMessage('Web doc is empty');
    Exit;
  end;
  Form2.Memo1.Lines.Clear;
  sl := TStringList.Create;

  n := doc1.frames.length;
  sl.Add('Frames: ' + IntToStr(n));
  // check each frame for the data
  if n = 0 then
    GetForms(doc1, sl)
  else
    for i := 0 to n - 1 do
    begin
      sl.Add('--Frame: ' + IntToStr(i));
      ole_index := i;
      frame_dispatch := doc1.Frames.Item(ole_index);
      if frame_dispatch <> nil then
      begin
        frame_win := frame_dispatch as IHTMLWindow2;
        doc2 := frame_win.document;
//        sl.Add(doc2.body.outerHTML);
        GetForms(doc2,sl);
        GetDoc(doc2);
      end;
    end;

// Form2 just contains a TMemo
  Form2.Memo1.Lines.AddStrings(sl);
  Form2.Show;
  sl.Free;
end;

您的示例中的逻辑是错误的,1。当网页上只有1个表单时,表单元素列表永远不会被提取,2。重复循环将导致访问冲突,除非“theid”中的标记“找到了

以下是您成功提取表单元素的示例。

var
  i : integer;
  nforms : integer;
  document : IHTMLDocument2;
  theForm : IHTMLFormElement;
  fields : TStringList;
  theform1 : integer;
  num : integer;
  theid : string;
begin
  fields := TStringList.Create;
  theid := 'xx';

// original code follows
i := -1;
//    nforms := NumberOfForms(webbrowser1.document as IHTMLDocument2);
//    document := webbrowser1.document as IHTMLDocument2;
//    if nforms = 1 then
//    begin
//      theForm := GetFormByNumber(webbrowser1.document as IHTMLDocument2, 0);
//      theform1 := 0;
//    end
//    else
    begin
//              repeat
              begin
                inc(i);
                theForm := GetFormByNumber(webbrowser1.document as IHTMLDocument2,
                  i);
                fields := GetFormFieldNames(theForm);
                num := fields.IndexOf(theid);
                theform1 := i;
              end;
//              until (num <> -1);
    end;
// end of original code

  Memo1.Lines.Text := fields.Text;
  fields.Free;
end;

答案 1 :(得分:0)

嗯,你确定这个链接包含任何表单元素吗?至少我没有看到任何可见的。也许它们是隐藏的 - 不过我自己也没看过。

迈克尔