如何通过 EmbeddedWB.FillForm 设置复选框的值? (德尔福)

发布于 2024-09-14 19:05:05 字数 165 浏览 5 评论 0原文

如何通过 FillForm 方法设置复选框的值? 我尝试了这些但不起作用:

  W.FillForm('Chkname', 'True');
  W.FillForm('Chkname', '1');
  W.FillForm('Chkname', '', 1);

how can i set value for a checkbox via FillForm method ?
I tried these but doesn't work :

  W.FillForm('Chkname', 'True');
  W.FillForm('Chkname', '1');
  W.FillForm('Chkname', '', 1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惜醉颜 2024-09-21 19:05:05

我知道,已经很晚了,但我会尝试回答这个问题,因为这是一个很好的问题,而且即使是当前版本的 < code>TMembeddedWB 没有实现此功能。

但是,您可以添加自己的函数来执行此操作;在下面的示例中,我使用 TEmbeddedWB 的插入类,其中我重载了FillForm 功能,支持复选框和单选按钮填充的版本。

如果您想设置复选框或选择某个单选按钮,请调用此版本的函数,其中:

  • FieldName(字符串)- 是元素的名称
  • Value(字符串)- 元素的值(可以为空,但在该值中)如果 FieldName 的第一个元素将被设置;Web 开发人员应该使用名称值对恕我直言)
  • 选择(布尔) - 如果为 True,则选中复选框或选择单选按钮

以下是代码: 这里是

uses
  EmbeddedWB, MSHTML;

type
  TEmbeddedWB = class(EmbeddedWB.TEmbeddedWB)
  public
    function FillForm(const FieldName, Value: string;
      Select: Boolean): Boolean; overload;
  end;

implementation

function TEmbeddedWB.FillForm(const FieldName, Value: string;
  Select: Boolean): Boolean;
var
  I: Integer;
  Element: IHTMLElement;
  InputElement: IHTMLInputElement;
  ElementCollection: IHTMLElementCollection;
begin
  Result := False;
  ElementCollection := (Document as IHTMLDocument3).getElementsByName(FieldName);
  if Assigned(ElementCollection) then
    for I := 0 to ElementCollection.length - 1 do
    begin
      Element := ElementCollection.item(I, '') as IHTMLElement;
      if Assigned(Element) then
      begin
        if UpperCase(Element.tagName) = 'INPUT' then
        begin
          InputElement := (Element as IHTMLInputElement);
          if ((InputElement.type_ = 'checkbox') or (InputElement.type_ = 'radio')) and
            ((Value = '') or (InputElement.value = Value)) then
          begin
            Result := True;
            InputElement.checked := Select;
            Break;
          end;
        end;
      end;
    end;
end;

一个基本的用法示例:

procedure TForm1.Button1Click(Sender: TObject);
var
  WebBrowser: TEmbeddedWB;
begin
  WebBrowser := TEmbeddedWB.Create(Self);
  WebBrowser.Parent := Self;
  WebBrowser.Align := alClient;
  WebBrowser.Navigate('http://www.w3schools.com/html/html_forms.asp');

  if WebBrowser.WaitWhileBusy(15000) then
  begin
    if not WebBrowser.FillForm('sex', 'male', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Bike', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Car', True) then
      ShowMessage('Error while form filling occured...');
  end;
end;

Quite late, I know, but I'll try to answer this since it's a good question and since even the current version of the TEmbeddedWB doesn't have this feature implemented.

However you can add your own function for doing this; in the following example I'm using the interposed class of TEmbeddedWB where I overloaded the FillForm function with the version which supports check box and radio button filling.

If you would like to set the check box or select some radio button call this version of function, where:

  • FieldName (string) - is the name of the element
  • Value (string) - value of the element (can be empty, but in that case the first element of the FieldName will be set; web developers should use name value pairs IMHO)
  • Select (Boolean) - if True, check box is checked or radio button selected

Here is the code:

uses
  EmbeddedWB, MSHTML;

type
  TEmbeddedWB = class(EmbeddedWB.TEmbeddedWB)
  public
    function FillForm(const FieldName, Value: string;
      Select: Boolean): Boolean; overload;
  end;

implementation

function TEmbeddedWB.FillForm(const FieldName, Value: string;
  Select: Boolean): Boolean;
var
  I: Integer;
  Element: IHTMLElement;
  InputElement: IHTMLInputElement;
  ElementCollection: IHTMLElementCollection;
begin
  Result := False;
  ElementCollection := (Document as IHTMLDocument3).getElementsByName(FieldName);
  if Assigned(ElementCollection) then
    for I := 0 to ElementCollection.length - 1 do
    begin
      Element := ElementCollection.item(I, '') as IHTMLElement;
      if Assigned(Element) then
      begin
        if UpperCase(Element.tagName) = 'INPUT' then
        begin
          InputElement := (Element as IHTMLInputElement);
          if ((InputElement.type_ = 'checkbox') or (InputElement.type_ = 'radio')) and
            ((Value = '') or (InputElement.value = Value)) then
          begin
            Result := True;
            InputElement.checked := Select;
            Break;
          end;
        end;
      end;
    end;
end;

And here a basic example of usage:

procedure TForm1.Button1Click(Sender: TObject);
var
  WebBrowser: TEmbeddedWB;
begin
  WebBrowser := TEmbeddedWB.Create(Self);
  WebBrowser.Parent := Self;
  WebBrowser.Align := alClient;
  WebBrowser.Navigate('http://www.w3schools.com/html/html_forms.asp');

  if WebBrowser.WaitWhileBusy(15000) then
  begin
    if not WebBrowser.FillForm('sex', 'male', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Bike', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Car', True) then
      ShowMessage('Error while form filling occured...');
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文