检查是否有< input>使用Delphi的HTML代码中的对象属性值 [英] Checking whether there are <input> object attribute values in the HTML Code using Delphi

查看:194
本文介绍了检查是否有< input>使用Delphi的HTML代码中的对象属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Delphi检查HTML代码中是否有输入对象属性值?

 没有值属性。 
< input name =input1type =text/>
有值属性。
< input name =input1type =textvalue =/>

我试过了以下内容:

<$ p如果WebBrowser1.OleObject.Document.GetElementByID('input1')。getAttribute('value')<> nil then
ShowMessage('value attribute is available')$ b,则$ p> $ b else
ShowMessage('value attribute isn't available')


解决方案

我以为我把这个问题作为答案,因为它花了我一段时间来编写一些代码来调查q中的评论中所说的内容,我想我可能会分享这种努力和结果,这并不是我所期望的。



从下面的结果看来,您无法分辨输入标签是否具有一个来自MSHTML DOM的value属性,因为如果它没有实际存在于HTML源代码中,DOM会合成一个。不确定这是否是OP希望的答案,但至少它会为您节省插入的麻烦一个新的属性节点,如果您想要
在代码中设置Input元素的值。如果otoh,您确实需要知道源中是否存在值
属性(这是原始q),那么您需要另一个解析器(可能是在本地执行的) - 如果页面的格式为XML,则可能是XML解析器 - 以下示例显示DOM报告:a)即使在源HTML(Input1)中不存在任何值属性,也存在value属性。 b)即使其节点值为空(Input2),也会命名为'value'的属性;并且c)Input1和Input2在DumpNode例程中应用的
基础上彼此不可区分。



给定部分DFM中的HTML:

  object moHtml:TMemo 
[...]
Lines.Strings =(
'< ; html>'
'< body>'
'< p>没有值属性。'
'< input name =input1type =text/> '
'< p>这是一个空值属性'
'< input name =input2type =textvalue =/>'
' '
'< input name =input3type =textvalue =已经有一个值''+
'/>'
'< / body>'
'< / html>')

以下代码报告:

 节点名称:INPUT 
值:
147:类型:>文字< ;
158:value:><
160:name:> input1<
节点名称:INPUT
值:
147:类型:>文本<
158:value:><
160:name:> input2<
节点名称:INPUT
值:
147:类型:>文本<
158:值:>已经具有值<
160:name:> input3<

代码:

  procedure TForm1.DumpItems; 
var
E:IHtmlElement;
D:IHtmlDomNode;

procedure DumpNode(ANode:IHtmlDomNode);
var
Attrs:IHtmlAttributeCollection;
A:IHtmlDomAttribute;
V:OleVariant;
我:整数;
begin
Log('Node name',ANode.nodeName);
V:= ANode.nodeValue;
如果不是VarIsNull(V)而不是VarIsEmpty(V)那么
Log('value',V)
else
Log('value','');

Attrs:= IDispatch(ANode.Attributes)作为IHtmlAttributeCollection;
for i:= 0 to Attrs.length - 1 do begin
V:= i;
A:= IDispatch(Attrs.item(V))作为IHtmlDomAttribute;
V:= A.nodeValue; (比较文本(A.nodeName,'Name')= 0)或(CompareText(A.nodeName,'Input')= 0)或(CompareText(A.nodeName,'Type')= 0)或
(CompareText(A.nodeName,'Value')= 0)然后开始
,如果不是VarIsNull(V)而不是VarIsEmpty(V),则
Log(''+ IntToStr(i)+':' + A.nodeName,'>'+ V +'<')
else
Log(''+ IntToStr(i)+':'+ A.nodeName,'')
结束;
end;

end;

begin
D:= IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input1'))作为IHtmlDomNode;
DumpNode(D);

D:= IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input2'))作为IHtmlDomNode;
DumpNode(D);

D:= IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input3'))作为IHtmlDomNode;
DumpNode(D);
end;

procedure TForm1.Log(const ALabel,AValue:String);
begin
Memo1.Lines.Add(ALabel +':'+ AValue);
end;

程序TForm1.btnLoadClick(发件人:TObject);
var
V:OleVariant;
Doc:IHtmlDocument2;
begin
WebBrowser1.Navigate('about:blank');
Doc:= WebBrowser1.Document作为IHTMLDocument2;
V:= VarArrayCreate([0,0],varVariant);
V [0]:= moHtml.Lines.Text;
Doc.Write(PSafeArray(TVarData(v).VArray));
Doc.Close;
end;

procedure TForm1.btnDumpClick(Sender:TObject);
begin
DumpItems;
end;


How do I check whether there are input object attribute values in HTML Code using Delphi?

there isn't value attribute.
<input name="input1" type="text"/>
there is value attribute.
<input name="input1" type="text" value=""/>

I've tried the following

  if WebBrowser1.OleObject.Document.GetElementByID('input1').getAttribute('value')<>nil then
       ShowMessage('value attribute is available')
     else
       ShowMessage('value attribute isn"t available')

解决方案

I thought that I'd put this up as an answer as it took me a while to put together some code to investigate what is said in the comments to the q, and I thought I might as well share that effort and the results, which were not what I'd been expecting.

It seems from the results below that you can't tell whether or not an input tag has a "value" attribute from the MSHTML DOM because the DOM "synthesizes" one if it's not physically present in the HTML source. Not sure if that was the answer the OP was hoping for, but at least it would save you the trouble of inserting a new attribute node if you were wanting to set the Input element's "value" in code. If otoh, you really need to know whether a value attribute is present in the source, which was the original q, then you need another parser, possibly home-rolled - maybe an XML parser if the page's format is XML-compliant.

The sample below shows that the DOM reports: a) the existence of a value attribute even when none is present in the source HTML (Input1); b) an attribute named 'value' even when its node value is empty (Input2); and that c) Input1 and Input2 are indistinguishable from one another on the basis applied in the DumpNode routine.

Given the HTML in this partial DFM:

object moHtml: TMemo
  [...]
  Lines.Strings = (
    '<html>'
    '  <body>'
    '    <p>This has no value attribute.'
    '    <input name="input1" type="text"/>'
    '    <p>This has an empty value attribute.'
    '    <input name="input2" type="text" value=""/>'
    '    <p>This has a value attribute.'
    '    <input name="input3" type="text" value="already has a value"' +
      '/>'
    '  </body>'
    '</html>')

The code below reports:

Node name: INPUT
     value: 
  147: type: >text<
  158: value: ><
  160: name: >input1<
Node name: INPUT
     value: 
  147: type: >text<
  158: value: ><
  160: name: >input2<
Node name: INPUT
     value: 
  147: type: >text<
  158: value: >already has a value<
  160: name: >input3<

Code:

procedure TForm1.DumpItems;
var
  E : IHtmlElement;
  D : IHtmlDomNode;

  procedure DumpNode(ANode : IHtmlDomNode);
  var
    Attrs : IHtmlAttributeCollection;
    A : IHtmlDomAttribute;
    V : OleVariant;
    i : Integer;
  begin
    Log('Node name', ANode.nodeName);
    V := ANode.nodeValue;
    if not VarIsNull(V) and not VarIsEmpty(V) then
      Log('     value', V)
    else
      Log('     value', '');

    Attrs := IDispatch(ANode.Attributes) as IHtmlAttributeCollection;
    for i := 0 to Attrs.length - 1 do begin
      V := i;
      A := IDispatch(Attrs.item(V)) as IHtmlDomAttribute;
      V := A.nodeValue;
      if (CompareText(A.nodeName, 'Name') = 0) or (CompareText(A.nodeName, 'Input') = 0) or (CompareText(A.nodeName, 'Type') = 0) or (CompareText(A.nodeName, 'Value') = 0) then begin
        if not VarIsNull(V) and not VarIsEmpty(V) then
          Log('  ' + IntToStr(i) + ': ' + A.nodeName, '>' + V + '<')
        else
          Log('  '  + IntToStr(i) + ': '+ A.nodeName, '')
        end;
    end;

  end;

begin
  D := IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input1')) as  IHtmlDomNode;
  DumpNode(D);

  D := IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input2')) as  IHtmlDomNode;
  DumpNode(D);

  D := IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input3')) as  IHtmlDomNode;
  DumpNode(D);
end;

procedure TForm1.Log(const ALabel, AValue : String);
begin
  Memo1.Lines.Add(ALabel + ': ' + AValue);
end;

procedure TForm1.btnLoadClick(Sender: TObject);
var
  V : OleVariant;
  Doc : IHtmlDocument2;
begin
  WebBrowser1.Navigate('about:blank');
  Doc := WebBrowser1.Document as IHTMLDocument2;
  V := VarArrayCreate([0, 0], varVariant);
  V[0] := moHtml.Lines.Text;
  Doc.Write(PSafeArray(TVarData(v).VArray));
  Doc.Close;
end;

procedure TForm1.btnDumpClick(Sender: TObject);
begin
  DumpItems;
end;

这篇关于检查是否有&lt; input&gt;使用Delphi的HTML代码中的对象属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆