c# - HtmlElement doesn't parse the tag properly -


i have following line in html source:

<input class="phone" name="site_url" type="text" placeholder="enter website url"> 

when navigate using webbrowser control (c#) , load site htmldocument object, , loop on each htmlelement, when input element above:

i can't placeholder attribute. getattribute("placeholder") returns "". checked outerhtml/innerhtml fields , noted placeholder attribute copied "" while other attributes not, moreover, can retrieve other attributes (name, class).

this output of innerhtml/outerhtml:

<input class=phone placeholder="enter website url" name=site_url> 

can explain why , how can change placeholder in case?

by default, webbrowser control runs in ie7 compatibility mode. in mode, placeholder attribute not supported. thus, first need switch ie10 mode, here's how. then, need call unmanaged getattributenode , value, here's how:

bool findelementwithplaceholder(htmlelement root, string placeholder, ref htmlelement found, ref string value) {     foreach (var child in root.children)     {         var childelement = (htmlelement)child;         dynamic domelement = childelement.domelement;         dynamic attrnode = domelement.getattributenode(placeholder);         if (attrnode != null)         {             string v = attrnode.value;             if (!string.isnullorwhitespace(v))             {                 value = v;                 found = childelement;                 return true;             }         }         if (findelementwithplaceholder(childelement, placeholder, ref found, ref value))             return true;     }     return false; }  // ...  htmlelement element = null; string value = null; if (findelementwithplaceholder(this.wb.document.body, "placeholder", ref element, ref value))     messagebox.show(value); 

this code has been tested ie10.

[edited] can still retrieve value of placeholder above code, if webbrowser feature control not emplemented. however, placeholder won't function visually in such case, because document won't in html5 mode.

[edited] perhaps, understand want. try code , see if that. still need feature control , doctype enable html5.

html: <!doctype html><html><input class=phone placeholder=\"enter website url\" name=site_url></html>  htmlelement element = null; string oldvalue = null; string newvalue = "new value"; findelementwithplaceholder(this.webbrowser1.document.body, "placeholder", ref element, ref value, newvalue);  bool findelementwithplaceholder(htmlelement root, string placeholder, ref htmlelement found, ref string oldvalue, string newvalue) {     foreach (var child in root.children)     {         var childelement = (htmlelement)child;         dynamic domelement = childelement.domelement;         dynamic attrnode = domelement.getattributenode(placeholder);         if (attrnode != null)         {             string v = attrnode.value;             if (!string.isnullorwhitespace(v))             {                 domelement.removeattributenode(attrnode);                 domelement.setattribute(placeholder, newvalue);                 // hack make ie10 render new placeholder                   var id = domelement.getattribute("id");                 var uniqueid = guid.newguid().tostring();                 domelement.setattribute("id", uniqueid);                 var html = domelement.outerhtml;                 domelement.outerhtml = html;                 var newelement = root.document.getelementbyid(uniqueid);                 domelement = newelement.domelement;                 if (string.isnullorempty(id))                     domelement.removeattribute("id");                 else                     domelement.setattribute("id", id);                 found = newelement;                 oldvalue = v;                 return true;             }         }         if (findelementwithplaceholder(childelement, placeholder, ref found, ref oldvalue, newvalue))             return true;     }     return false; } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -