在 delphi XE2 中检测 Twebbrowser 输入框点击
Detect Twebbrowser inputbox click in delphi XE2
我正在考虑为 Delphi 创建自动表单填充器,显然需要一个好的方法来捕获每个站点上哪些输入框是登录框,所以想知道我是否使用 Twebbrowser 组件并加载页面然后单击特定站点上的用户名和密码框,如果我可以提取我单击的表单名称和输入框名称。
简而言之,我需要 delphi 捕获加载到 twebbrowser 组件中的网页上所选输入框的名称。
任何从 twebbrowser 页面中加载的页面捕获此信息的好方法将不胜感激!
抱歉我的格式问题,新来这里发帖!。缩小版。
procedure TForm5.Button2Click(Sender: TObject);
var
Document: IHTMLdocument2;
MyEl: IHTMLElement;
begin
MyEl := (WebBrowser1.Document as IHTMLDocument2).activeElement;
If MyEl.tagName = 'INPUT' then
begin
edit2.Text := MyEl.getAttribute('Name', 0);
end;
end;
以下代码显示如何查找名为 'input1':
的 INPUT 元素
var
E : IHtmlElement;
D : IHtmlDomNode;
Doc2 : IHtmlDocument2;
Doc3 : IHtmlDocument3;
All : IHTMLElementCollection;
i : Integer;
begin
Doc3 := WebBrowser1.Document as IHtmlDocument3;
D := Doc3.GetElementByID('input1') as IHtmlDomNode;
if D <> Nil then begin
...
如果您需要查找多个 INPUT 元素或希望对
INPUT 元素的名称,您可以通过检索文档的
IHtmlDocument2 接口,然后迭代其 all
集合:
Doc2 := WebBrowser1.Document as IHtmlDocument2;
All := Doc2.all;
for i := 0 to All.Length - 1 do begin
E := All.Item(Null, i) as IHtmlElement;
// Test E and do what you like with it
end;
您可以使用这样的函数来查找 INPUT 元素的父 FORM 元素
function GetParentFormElement(E : IHtmlElement) : IHtmlElement;
begin
Result := Nil;
while E <> Nil do begin
if CompareText(E.tagName, 'form') = 0 then begin
Result := E;
exit;
end;
E := E.parentElement;
end;
end;
并像这样使用它:
E := D as IHtmlElement;
E := GetParentFormElement(E);
Assert(E <> Nil);
not all forms have a name or id so how do I get the number or reference of a parent form if there are a number of forms in a page?
同样,并非所有 INPUT
个元素都包含在 FORM
个元素中。 TBH,我不知道有什么健壮的方法可以做你想做的事情,这种方法可以在页面作者对其进行更改后继续存在。无论如何,必须有 some 方法来识别给定的 INPUT
元素,否则服务器将无法提取用户的响应,不是吗?所以这只是一个弄清楚特定页面可能是什么的问题。它不在元素的属性中,那么也许您可以查找附近文本元素的文本 - 毕竟,必须有某种提示提示用户告诉他们在哪里填写什么。但这确实与您原来的问题的实质不同,我希望我已经回答了。如果您在这一点上需要更多帮助,我建议您在新的问题中提问。确保你包含了你已经尝试过的细节(代码),因为缺少它们的问题往往不会在 SO 得到很好的接受。
我正在考虑为 Delphi 创建自动表单填充器,显然需要一个好的方法来捕获每个站点上哪些输入框是登录框,所以想知道我是否使用 Twebbrowser 组件并加载页面然后单击特定站点上的用户名和密码框,如果我可以提取我单击的表单名称和输入框名称。
简而言之,我需要 delphi 捕获加载到 twebbrowser 组件中的网页上所选输入框的名称。
任何从 twebbrowser 页面中加载的页面捕获此信息的好方法将不胜感激!
抱歉我的格式问题,新来这里发帖!。缩小版。
procedure TForm5.Button2Click(Sender: TObject);
var
Document: IHTMLdocument2;
MyEl: IHTMLElement;
begin
MyEl := (WebBrowser1.Document as IHTMLDocument2).activeElement;
If MyEl.tagName = 'INPUT' then
begin
edit2.Text := MyEl.getAttribute('Name', 0);
end;
end;
以下代码显示如何查找名为 'input1':
的 INPUT 元素var
E : IHtmlElement;
D : IHtmlDomNode;
Doc2 : IHtmlDocument2;
Doc3 : IHtmlDocument3;
All : IHTMLElementCollection;
i : Integer;
begin
Doc3 := WebBrowser1.Document as IHtmlDocument3;
D := Doc3.GetElementByID('input1') as IHtmlDomNode;
if D <> Nil then begin
...
如果您需要查找多个 INPUT 元素或希望对
INPUT 元素的名称,您可以通过检索文档的
IHtmlDocument2 接口,然后迭代其 all
集合:
Doc2 := WebBrowser1.Document as IHtmlDocument2;
All := Doc2.all;
for i := 0 to All.Length - 1 do begin
E := All.Item(Null, i) as IHtmlElement;
// Test E and do what you like with it
end;
您可以使用这样的函数来查找 INPUT 元素的父 FORM 元素
function GetParentFormElement(E : IHtmlElement) : IHtmlElement;
begin
Result := Nil;
while E <> Nil do begin
if CompareText(E.tagName, 'form') = 0 then begin
Result := E;
exit;
end;
E := E.parentElement;
end;
end;
并像这样使用它:
E := D as IHtmlElement;
E := GetParentFormElement(E);
Assert(E <> Nil);
not all forms have a name or id so how do I get the number or reference of a parent form if there are a number of forms in a page?
同样,并非所有 INPUT
个元素都包含在 FORM
个元素中。 TBH,我不知道有什么健壮的方法可以做你想做的事情,这种方法可以在页面作者对其进行更改后继续存在。无论如何,必须有 some 方法来识别给定的 INPUT
元素,否则服务器将无法提取用户的响应,不是吗?所以这只是一个弄清楚特定页面可能是什么的问题。它不在元素的属性中,那么也许您可以查找附近文本元素的文本 - 毕竟,必须有某种提示提示用户告诉他们在哪里填写什么。但这确实与您原来的问题的实质不同,我希望我已经回答了。如果您在这一点上需要更多帮助,我建议您在新的问题中提问。确保你包含了你已经尝试过的细节(代码),因为缺少它们的问题往往不会在 SO 得到很好的接受。