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

问题描述:

我怎样才能通过FillForm方法设置复选框的值? 我想这可是不行的:如何通过EmbeddedWB.FillForm设置复选框的值? (德尔福)

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

哪里你'FillForm'从何而来?我不记得这是一个标准的德尔福功能。它是什么附加单元/库? – 2010-08-20 18:49:32

+0

嵌入式Web浏览器:http://www.bsalsa.com – Kermia 2010-08-20 20:43:33

比较晚,我知道,但我会尽力回答这个,因为它是一个很好的问题,因为即使TEmbeddedWB的当前版本没有此功能已实施。

但是,您可以添加自己的功能来做到这一点;在下面的例子中,我使用了插入类TEmbeddedWB,其中我使用支持复选框和单选按钮填充的版本过载了FillForm函数。

如果你想设置的复选框,或者选择一些单选按钮调用这个版本的功能,其中:

  • 字段名(字符串) - 是元素的名称
  • 值(字符串) - 元素的值(可以是空的,但在这种情况下,将设置FieldName的第一个元素; Web开发人员应该使用名称值对IMHO)
  • Select(布尔) - 如果为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; 
+0

这篇文章的主要观点是,你不能使用'TEmbeddedWB.FillForm',因为它设置了元素的'value'属性不应该这样做,因为复选框和单选按钮等元素在发送表单时具有用于构建名称值对的值。 – TLama 2012-03-07 11:43:46