GetFormFieldNames 并不总是有效
我试图找出哪种形式和元素也属于。我现在从这个网站理解的代码:
http://www.cryer .co.uk/brian/delphi/twebbrowser/read_write_form_elements.htm
包含此代码的
function GetFormFieldNames(fromForm: IHTMLFormElement): TStringList;
var
index: integer;
field: IHTMLElement;
input: IHTMLInputElement;
select: IHTMLSelectElement;
text: IHTMLTextAreaElement;
begin
result := TStringList.Create;
for index := 0 to fromForm.length do
begin
field := fromForm.Item(index,'') as IHTMLElement;
if Assigned(field) then
begin
if field.tagName = 'INPUT' then
begin
// Input field.
input := field as IHTMLInputElement;
result.Add(input.name);
end
else if field.tagName = 'SELECT' then
begin
// Select field.
select := field as IHTMLSelectElement;
result.Add(select.name);
end
else if field.tagName = 'TEXTAREA' then
begin
// TextArea field.
text := field as IHTMLTextAreaElement;
result.Add(text.name);
end;
end;
end;
end;
似乎对大多数网站都工作正常。但是,有一些网站,例如以下网站:
http://service .mail.com/registration.html#.1258-bluestripe-product1-undef
通过查看该代码并将其与活动 ID 进行比较,我可以找到它所在的形式。但是它不适用于该形式网站。出于某种原因,我认为它与 htmldocument3 有关,并且该代码适用于 htmldocument2。但我不确定。
所以我的问题是如何从该网站中提取包含所有元素名称的 tstringlist ?希望你能帮忙!
编辑:添加了一些代码
begin
theForm := GetFormByNumber(webbrowser1.document as IHTMLDocument2,
0);
fields := GetFormFieldNames(theForm);
num := fields.IndexOf(theid);
end;
until (num <> -1);
I am trying to find out which form and element belongs too. The code that I now understand from this website:
http://www.cryer.co.uk/brian/delphi/twebbrowser/read_write_form_elements.htm
containing this code
function GetFormFieldNames(fromForm: IHTMLFormElement): TStringList;
var
index: integer;
field: IHTMLElement;
input: IHTMLInputElement;
select: IHTMLSelectElement;
text: IHTMLTextAreaElement;
begin
result := TStringList.Create;
for index := 0 to fromForm.length do
begin
field := fromForm.Item(index,'') as IHTMLElement;
if Assigned(field) then
begin
if field.tagName = 'INPUT' then
begin
// Input field.
input := field as IHTMLInputElement;
result.Add(input.name);
end
else if field.tagName = 'SELECT' then
begin
// Select field.
select := field as IHTMLSelectElement;
result.Add(select.name);
end
else if field.tagName = 'TEXTAREA' then
begin
// TextArea field.
text := field as IHTMLTextAreaElement;
result.Add(text.name);
end;
end;
end;
end;
seems to be working fine for most sites. However there are a few websites such as this one:
http://service.mail.com/registration.html#.1258-bluestripe-product1-undef
By looking at that code and comparing it with the active id, I can find the form it is in. However it does not work for that website. for some reason I think it has to do with htmldocument3 adn that this code is for htmldocument2. But I am not sure.
so my question is How can I extract a tstringlist from this website with all the elements names in them? hope you can help!
Edited: Added some code
begin
theForm := GetFormByNumber(webbrowser1.document as IHTMLDocument2,
0);
fields := GetFormFieldNames(theForm);
num := fields.IndexOf(theid);
end;
until (num <> -1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在网页中定位表单元素的一个复杂问题是该页面可能包含框架并且任何框架中都可能存在表单。基本上,您必须迭代所有框架和每个框架中的表单。一旦获得 IHTMLFormElement 形式的表单,就可以使用 Cryer 的函数来获取表单元素名称。
您提供的示例链接没有任何框架,并且您在获取表单元素列表时应该没有问题,除非您尝试按名称获取表单,因为它没有分配名称。 获取表单元素名称和值没有问题
使用以下过程Cryer 的示例 导航框架集不适用于所有网站,请参阅http://support.microsoft.com/support/kb/articles/Q196/3/40.ASP。以下函数在我尝试过的所有网站上成功提取框架作为 IHTMLDocument2
这是我如何使用 GetForms 和 GetFrameByNumber 的示例
您的示例中的逻辑是错误的,1.当网页上只有 1 个表单时,列表表单元素永远不会被提取,2.重复循环将导致访问冲突,除非找到“theid”中的标签
这是您成功提取表单元素的示例。
One complication with locating form elements in a web page is that the page may contain frames and there may be forms in any of the frames. Basically, you have to iterate through all the frames and the forms in each frame. Once you get the form as an IHTMLFormElement, use Cryer's function to get the form element names.
The example link you gave does not have any frames and you should have had no problems getting your list of form elements, unless you tried to get the form by name because it had no name assigned. I had no problem getting the form element names and values using the following procedure
Cryer's example for navigating a frameset will not work for all web sites, see http://support.microsoft.com/support/kb/articles/Q196/3/40.ASP. The following function successfuly extracts a frame as an IHTMLDocument2 on all sites I have tried
Here is an example of how I have used GetForms and GetFrameByNumber
The logic in your example is faulty, 1. when there is only 1 form on the web page the list of form elements is never extracted, 2. the repeat loop will result in a access violation unless the the tag in "theid" is found
Here is your example cut down to successfully extract the form elements.
嗯,您确定此链接包含任何表单元素吗?至少我没有看到任何可见的。也许它们是隐藏的——但是我自己没有检查过。
迈克尔
Hm, are you sure this link contains any form elements? At least I did not see any visible ones. Perhaps they are hidden - did not check this myself, however.
Michael