[英]VB.NET / C#.Net MSHTML: Unable to get “name” attribute from Outerhtml after using “setAttribute('name',value)” for certain elements
I am developing a WYSIWYG application specifically for my company usage with custom integration with company's existing tools. 我正在开发一种所见即所得的应用程序,专门用于与公司现有工具进行自定义集成的公司用途。
I was unable to get the "name" attribute out of certain elements when trying to get the html string by using ".OuterHtml", especially INPUT tag element. 当尝试通过使用“ .OuterHtml”(尤其是INPUT标签元素)获取html字符串时,无法从某些元素中获取“ name”属性。
`Dim inElem as windows.forms.htmlElement = hdoc.CreateElement("INPUT")`
`inElem.Id = "txt01"`
`inElem.setAttribute("name", inElem.Id)`
`inElem.setAttribute("type", "text")`
`inElem.setAttribute("placeholder","text here....")`
'' append the created element to html body
`hdoc.Body.AppendChild(inElem)`
--> Getting html string:
** hdoc.body.getElementById("txt01").OuterHtml => "<input id=txt01 placeholder='text here....'></input>"
--> What I really want is:
** hdoc.body.getElementById("txt01").OuterHtml => "<input id=txt01 placeholder='text here....' type='text' name='txt01'></input>"
Yes, not only name attribute were missing, some other too. 是的,不仅缺少名称属性,还有一些其他属性。 (eg TYPE) Anyone could help me on this matter? (例如TYPE)有人可以帮助我吗?
For Each inputEle As Windows.Forms.HtmlElement In hdoc.Body.GetElementsByTagName("input")
CType(inputEle.DomElement, mshtml.IHTMLInputElement).name = inputEle.Id
Next
** FAILED ** :( **失败** :(
Use HTML Agility Pack:
----------------------
Dim inputEle3 As HtmlAgilityPack.HtmlNode = new_wb.CreateElement("input")
inputEle3.Attributes.Add("id", "txt01")
inputEle3.Attributes.Add("name", inputEle3.Id)
inputEle3.Attributes.Add("type", "text")
inputEle3.Attributes.Add("placeholder", "text here ....")
RESULT:
-------
inputEle3.OuterHtml => <input id="txt01" name="txt01" type="text" placeholder="text here ...." >
It works now, provided I use HtmlAgilityPack.dll :( Microsoft mshtml sucks! :( 只要我使用HtmlAgilityPack.dll :(微软mshtml很烂!:(
This is what worked for me. 这对我有用。 Forgive me using dynamic
datatype, I do not have the mshtml
library on my Visual Studio for some reason. 原谅我使用dynamic
数据类型,由于某种原因我在Visual Studio上没有mshtml
库。
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("about:blank");
this.webBrowser1.Document.Write("<INPUT id='hell' class='blah' placeholder='text here' name='hell' type='text'></INPUT>");
dynamic htmldoc = webBrowser1.Document.DomDocument as dynamic;
dynamic node = htmldoc.getElementById("hell") as dynamic;
string x = node.OuterHtml; //gets name but not type
string s = node.GetAttribute["type"]; //gets type
string name = node.GetAttribute["name"]; //gets name
}
So the OuterHtml per say did not get the attribute, but when calling the GetAttribute method it did work. 所以说的OuterHtml没有获得属性,但是在调用GetAttribute方法时它确实起作用。 Hopefuly this helps. 希望这会有所帮助。
Use HTML Agility Pack:
----------------------
Dim inputEle3 As HtmlAgilityPack.HtmlNode = new_wb.CreateElement("input")
inputEle3.Attributes.Add("id", "txt01")
inputEle3.Attributes.Add("name", inputEle3.Id)
inputEle3.Attributes.Add("type", "text")
inputEle3.Attributes.Add("placeholder", "text here ....")
RESULT:
-------
inputEle3.OuterHtml => <input id="txt01" name="txt01" type="text" placeholder="text here ...." >
It works now, provided I use HtmlAgilityPack.dll :( Microsoft mshtml sucks! :( 只要我使用HtmlAgilityPack.dll :(微软mshtml很烂!:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.