Checking for <input> Object Attribute Values ​​in HTML Using Delphi

How to check if there are attribute values ​​of input objects 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 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') 
+6
source share
1 answer

I thought I put this as the answer, since it took me a while to compile the code to examine what was said in the comments on q, and I thought that I could share these efforts and the results that I did not expect.

From the results below, it can be seen that you cannot determine whether the input tag has a “value” attribute from the MSHTML DOM, because the DOM “synthesizes” one if it is not physically present in the HTML source. Not sure if this was the answer the OP was hoping for, but at least it would save you the trouble of inserting a new node attribute if you want to set the “Input Element” value in your code. If otoh, you really need to know if the attribute has a value in the source, which was the source q, then you need another parser, perhaps built-in to the house - perhaps an XML parser if the page format complies with the XML requirements.

The example below shows that the DOM reports: a) the existence of the value attribute, even if there is not one in the source HTML (Input1); b) an attribute named “value”, even if its node value is empty (Input2); and that c) Input1 and Input2 are indistinguishable from each other on the basis used 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>') 

Below is the code below:

 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< 

the 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; 
0
source

Source: https://habr.com/ru/post/913395/


All Articles