使用TWebBrowser处理Web表单

The TWebBrowser Delphi control provides access to the Web browser functionality from your Delphi apps - to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.

TWebBrowser Delphi控件可从您的Delphi应用程序访问Web浏览器功能-允许您创建自定义的Web浏览应用程序或向应用程序添加Internet,文件和网络浏览,文档查看和数据下载功能。

网络表格 ( Web Forms )

A web form or a form on a web page allows a web page visitor to enter data that is, in most cases, sent to the server for processing.

Web窗体 或网页上表单允许网页访问者输入是,在大多数情况下,发送到服务器进行处理的数据。

The simplest web form could consist of one input element (edit control) and a submit button. Most web search engines (like Google) use such a web form to allow you to search the internet.

最简单的Web表单可以包含一个输入元素 (编辑控件)和一个提交按钮。 大多数网络搜索引擎(例如Google)都使用这种网络表单来允许您搜索互联网。

More complex web forms would include drop-down lists, check boxes, radio buttons, etc. A web form is much like a standard windows form with text input and selection controls.

更复杂的Web表单将包括下拉列表,复选框,单选按钮等。Web表单非常类似于带有文本输入和选择控件的标准Windows表单。

Every form would include a button - a submit button - that tells the browser to take action on the web form (typically to send it to a web server for processing).

每个表单都将包含一个按钮-提交按钮-告诉浏览器对Web表单执行操作(通常是将其发送到Web服务器进行处理)。

以编程方式填充Web表单 ( Programmatically Populating Web Forms )

If in your desktop application you use the TWebBrowser to display web pages, you can programmatically control web forms: manipulate, change, fill, populate fields of a web form and submit it.

如果在桌面应用程序中使用TWebBrowser来显示网页,则可以以编程方式控制Web表单:操纵,更改,填充,填充Web表单的字段并提交。

Here's a collection of custom Delphi functions you can use to list all the web forms on a web page, to retrieve input elements, to programmatically populate fields and to finally submit the form.

这是自定义Delphi函数的集合,可用于在网页上列出所有Web表单,检索输入元素,以编程方式填充字段并最终提交表单。

To more easily follow the examples, let's say there's a TWebBrowser control named "WebBrowser1" on a Delphi (standard Windows) form.

为了更轻松地遵循示例,我们假设在Delphi(标准Windows)窗体上有一个名为“ WebBrowser1”的TWebBrowser控件。

Note: you should add mshtml to your uses clause in order to compile the methods listed here.

注意:您应将mshtml添加到uses子句中,以便编译此处列出的方法。

列出Web表单名称,按索引获取Web表单 ( List Web Form Names, Get a Web Form by Index )

A web page would in most cases have only one web form, but some web pages might have more than one web form. Here's how to get the names of all the web forms on a web page:

一个网页在大多数情况下将只有一个Web表单,但是某些网页可能具有多个Web表单。 以下是获取网页上所有网络表单名称的方法:

function WebFormNames(const document: IHTMLDocument2): TStringList;
var
  forms : IHTMLElementCollection;
  form : IHTMLFormElement;
  idx : integer;
begin
  forms := document.Forms as IHTMLElementCollection;
  result := TStringList.Create;
  for idx := 0 to -1 + forms.length do
  begin
    form := forms.item(idx,0) as IHTMLFormElement;
    result.Add(form.name) ;
  end;
end;

A simple usage to display the list of web form names in a TMemo:

在TMemo中显示Web表单名称列表的简单用法:

var
  forms : TStringList;
begin
  forms := WebFormNames(WebBrowser1.Document AS IHTMLDocument2) ;
  try
    memo1.Lines.Assign(forms) ;
  finally
    forms.Free;
  end;
end; 

Here's how to get the instance of a web form by index. For a single form page the index would be 0 (zero).

这是通过index获取Web表单实例的方法 。 对于单个表单页面,索引将为0(零)。

function WebFormGet(const formNumber: integer; const document: IHTMLDocument2): IHTMLFormElement;
var
  forms : IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  result := forms.Item(formNumber,'') as IHTMLFormElement
end; 

Once you have the web form, you can list all the HTML input elements by their name, you can get or set the value for each of the fields, and finally, you can submit the web form.

有了Web表单后,您可以按名称列出所有HTML输入元素 ,可以获取或设置每个字段的值 ,最后可以提交Web表单

Web pages can host web forms with input elements like edit boxes and drop down lists which you can control and manipulate programmatically from Delphi code.

网页可以托管带有输入元素(例如编辑框和下拉列表)的Web表单,您可以从Delphi代码中以编程方式控制和操作这些表单。

Once you have the web form, you can list all the HTML input elements by their name:

有了网络表单后,您可以按名称列出所有HTML输入元素

function WebFormFields(const document: IHTMLDocument2; const formName : string): TStringList; var   form : IHTMLFormElement;   field : IHTMLElement;   fName : string;   idx : integer; begin   form := WebFormGet(0, WebBrowser1.Document AS IHTMLDocument2) ;   result := TStringList.Create;   for idx := 0 to -1 + form.length do  begin     field := form.item(idx, '') as IHTMLElement;     if field = nil then Continue;     fName := field.id;     if field.tagName = 'INPUT' then fName := (field as IHTMLInputElement).name;     if field.tagName = 'SELECT' then fName := (field as IHTMLSelectElement).name;     if field.tagName = 'TEXTAREA' then fName := (field as IHTMLTextAreaElement).name;     result.Add(fName) ;   endend;

When you know the names of the fields on a web form, you can programmatically get the value for a single HTML field:

当您知道Web表单上的字段名称时,可以以编程方式获取单个HTML字段的值

function WebFormFieldValue(   const document: IHTMLDocument2;   const formNumber : integer;   const fieldName : string): stringvar   form : IHTMLFormElement;   field: IHTMLElement; begin   form := WebFormGet(formNumber, WebBrowser1.Document AS IHTMLDocument2) ;   field := form.Item(fieldName,'') as IHTMLElement;   if field = nil then Exit;   if field.tagName = 'INPUT' then result := (field as IHTMLInputElement).value;   if field.tagName = 'SELECT' then result := (field as IHTMLSelectElement).value;   if field.tagName = 'TEXTAREA' then result := (field as IHTMLTextAreaElement).value; end;

An example of usage to get the value of an input field named "URL":

获取名为“ URL”的输入字段的值的用法示例:

const   FIELDNAME = 'url'; var   doc :IHTMLDocument2;   fieldValue : stringbegin  doc := WebBrowser1.Document AS IHTMLDocument2;   fieldValue := WebFormFieldValue(doc, 0, FIELDNAME) ;   memo1.Lines.Add('Field : "URL", value:' + fieldValue) ;end;

The entire idea would have no value if you would not be able to fill in web form elements:

如果您无法填写Web表单元素 ,则整个想法将毫无价值

procedure WebFormSetFieldValue(const document: IHTMLDocument2; const formNumber: integer; const fieldName, newValue: string) ; var   form : IHTMLFormElement;   field: IHTMLElement; begin   form := WebFormGet(formNumber, WebBrowser1.Document AS IHTMLDocument2) ;   field := form.Item(fieldName,'') as IHTMLElement;   if field = nil then Exit;   if field.tagName = 'INPUT' then (field as IHTMLInputElement).value := newValue;   if field.tagName = 'SELECT' then (field as IHTMLSelectElement) := newValue;   if field.tagName = 'TEXTAREA' then (field as IHTMLTextAreaElement) := newValue; end;

提交网络表格 ( Submit a Web Form )

Finally, when all the fields are manipulated, you would probably want to submit the web form from Delphi code. Here's how:

最后,在处理完所有字段之后,您可能希望通过Delphi代码提交Web表单。 这是如何做:

procedure WebFormSubmit(const document: IHTMLDocument2;   const formNumber: integer) ; var   form : IHTMLFormElement;   field: IHTMLElement; begin   form := WebFormGet(formNumber, WebBrowser1.Document AS IHTMLDocument2) ;   form.submit; end;

Not All Web Forms Are "Open Minded"

并非所有的Web表单都是“思想开放的”

Some web forms might host a captcha image to prevent web pages from being manipulated programmatically.

某些Web表单可能会托管一个验证码图像,以防止以编程方式处理网页。

Some web forms might not be submitted when you "click the submit button." Some web forms execute JavaScript or some other procedure gets executed handled by the "onsubmit" event of the web form.

当您“单击提交按钮”时,某些Web表单可能不会提交。 某些Web表单执行JavaScript,或者通过Web表单的“ onsubmit”事件处理某些其他过程。

In any event, web pages can be controlled programmatically, the only question is "how far are you prepared to go?"

无论如何,都可以通过编程方式控制网页,唯一的问题是“您准备走多远?”

翻译自: https://www.thoughtco.com/manipulate-web-forms-using-the-twebbrowser-1058362

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值