以下是示范如何读取及提交TWebBrowser中的网页

示例内容:

  • 获得网页中表单(form)的数量

  • 按编号从页面中获取表单

  • 按编号得到表单的名称

  • 按名称提取表单

  • 获取表单中所有字段的名称

  • 获取表单中指定名称的字段的值

  • 设置表单中指定名称的字段的值

  • 提交表单

  • 其它演示

以下的示例假设TWebBrowser被命名为WebBrowser而且网页被已经打开.


获得网页中表单(form)的数量

function NumberOfForms(document: IHTMLDocument2): integer;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  result := forms.Length;
end;

按编号从页面中获取表单

网页中可能会有多个表单,表单的编号从零开始

 

function GetFormByNumber(document: IHTMLDocument2;
    formNumber: integer): IHTMLFormElement;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  if formNumber < forms.Length then
    result := forms.Item(formNumber,'') as IHTMLFormElement
  else
    result := nil;
end;

按编号得到表单的名称

 

var
  firstForm: IHTMLFormElement;
  document: IHTMLDocument2;
begin
  document := WebBrowser.Document as IHTMLDocument2;
  firstForm := GetFormByNumber(document,0);
  if Assigned(firstForm) then
    ShowMessage('这个表单的名称是' + firstForm.Name)
  else
    ShowMessage(这个页面不包含任何表单');

按名称提取表单

如果知道表单的名称就可以直接按名称提取表单

function GetFormByName(document: IHTMLDocument2;
    const formName: string): IHTMLFormElement;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  result := forms.Item(formName,'') as IHTMLFormElement
end;

如果网页中不包含表单则返回nil


获取表单中所有字段的名称

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;

应用:

procedure TMyForm.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := TWebBrowser.Document as IHTMLDocument2;
  theForm := GetFormByNumber(WebBrowser.Document as IHTMLDocument2,0);
  fields := GetFormFieldNames(theForm);

  for index := 0 to fields.count-1 do
    ShowMessage('Field ' + IntToStr(index) + ' called ' + fields[index]);
end;

获取表单中指定名称的字段的值

 

function GetFieldValue(fromForm: IHTMLFormElement;
  const fieldName: string): string;
var
  field: IHTMLElement;
  inputField: IHTMLInputElement;
  selectField: IHTMLSelectElement;
  textField: IHTMLTextAreaElement;
begin
  field := fromForm.Item(fieldName,'') as IHTMLElement;
  if not Assigned(field) then
    result := ''
  else if field.tagName = 'INPUT' then
  begin
    inputField := field as IHTMLInputElement;
    if (inputField.type_ <> 'radio') and
       (inputField.type_ <> 'checkbox')
    then
      result := inputField.value
    else if inputField.checked then
      result := 'checked'
    else
      result := 'unchecked';
  end
  else if field.tagName = 'SELECT' then
  begin
    selectField := field as IHTMLSelectElement;
    result := selectField.value
  end
  else if field.tagName = 'TEXTAREA' then
    begin
    textField := field as IHTMLTextAreaElement;
    result := textField.value;
  end;
end;

应用:

procedure TMyForm.Button1Click(Sender: TObject);      
var      
  document: IHTMLDocument2;      
  theForm: IHTMLFormElement;      
  index: integer;      
begin      
  document := TWebBrowser.Document as IHTMLDocument2;      
  theForm := GetFormByNumber(WebBrowser.Document as IHTMLDocument2,0);      
  ShowMessage('Field "name" has value ' + GetFieldValue(theForm,'name'));

"GetFieldValue" 函数在猎取字段值之前先判断该字段的类型,如果已知字段类型,该函数可以被简化,

举例说如果你确定字段是一个input字段,

function GetInputField(fromForm: IHTMLFormElement;      
  const inputName: string;      
  const instance: integer=0): HTMLInputElement;      
var      
  field: IHTMLElement;      
begin      
  field := fromForm.Item(inputName,instance) as IHTMLElement;      
  if Assigned(field) then      
  begin      
    if field.tagName = 'INPUT' then      
    begin      
      result := field as HTMLInputElement;      
      exit;      
    end;      
  end;      
  result := nil;      
end;


设置表单中指定名称的字段的值

 

procedure SetFieldValue(theForm: IHTMLFormElement;
  const fieldName: string; const newValue: string;
  const instance: integer=0);
var
  field: IHTMLElement;
  inputField: IHTMLInputElement;
  selectField: IHTMLSelectElement;
  textField: IHTMLTextAreaElement;
begin
  field := theForm.Item(fieldName,instance) as IHTMLElement;
  if Assigned(field) then
  begin
    if field.tagName = 'INPUT' then
    begin
      inputField := field as IHTMLInputElement;
      if (inputField.type_ <> 'radio') and
         (inputField.type_ <> 'checkbox')
      then
        inputField.value := newValue
      else
        inputField.checked := (newValue = 'checked');
    end
    else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      selectField.value := newValue;
    end
    else if field.tagName = 'TEXTAREA' then
    begin
      textField := field as IHTMLTextAreaElement;
      textField.value := newValue;
    end;
  end;
end;

应用:

procedure TMyForm.Button1Click(Sender: TObject);      
var      
  document: IHTMLDocument2;      
  theForm: IHTMLFormElement;      
  index: integer;      
begin      
  document := TWebBrowser.Document as IHTMLDocument2;      
  theForm := GetFormByNumber(WebBrowser.Document as IHTMLDocument2,0);      
  SetFieldValue(theForm,'name','Brian Cryer');


提交表单

最后一件事就是向服务器提交表单:

procedure TMyForm.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := TWebBrowser.Document as IHTMLDocument2;
  theForm := GetFormByNumber(document,0);
  SetFieldValue(theForm,'name','Brian Cryer');
  theForm.submit;