博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi下WebBrowser应用示例
阅读量:4482 次
发布时间:2019-06-08

本文共 5869 字,大约阅读时间需要 19 分钟。

以下是示范如何读取及提交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 IHTMLFormElementend;

如果网页中不包含表单则返回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;

转载于:https://www.cnblogs.com/karinthy/archive/2011/10/25/2224187.html

你可能感兴趣的文章
numpy的使用学习
查看>>
Python_代码练习_写一个判断是否为小数的函数
查看>>
1044 Shopping in Mars (25 分)
查看>>
Linux系统编程——静态库和动态库
查看>>
MySQL管理工具 -- MySQL Workbench
查看>>
Mad Libs
查看>>
Django 请求类型
查看>>
机器学习笔试面试系列算法集锦
查看>>
C#委托Action、Action<T>、Func<T>、Predicate<T>
查看>>
Android Studio使用出现的问题
查看>>
Sublime Text 3设置指南
查看>>
Android学习笔记(十一) Intent
查看>>
移动端网站的内容触摸滑动
查看>>
遇到中文传参乱码的情况,究竟应该如何解决?
查看>>
解析KML文件并提取coordinates中的经纬度坐标信息
查看>>
Nginx反向代理配置文件
查看>>
当页面上需要的字段不在model中时候,需要自行设置该字段
查看>>
前端极易被误导的css选择器权重计算及css内联样式的妙用技巧
查看>>
webpack+sass+vue 入门教程(三)
查看>>
ValidationUtil
查看>>