[英]How to set value for checkbox via EmbeddedWB.FillForm ? (Delphi)
how can i set value for a checkbox via FillForm method ? 如何通过FillForm方法为复选框设置值? I tried these but doesn't work :
我试过这些但不起作用:
W.FillForm('Chkname', 'True');
W.FillForm('Chkname', '1');
W.FillForm('Chkname', '', 1);
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. 很晚,我知道,但我会尝试回答这个,因为这是一个很好的问题,因为即使是当前版本的
TEmbeddedWB
也没有实现这个功能。
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. 在下面的示例中,我使用插入的
TEmbeddedWB
类,其中我使用支持复选框和单选按钮填充的版本重载了FillForm
函数。
If you would like to set the check box or select some radio button call this version of function, where: 如果您想设置复选框或选择一些单选按钮,请调用此版本的功能,其中:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.