如何将mshtml.IHTMLDivElement转换为mshtml.HTMLDivElementClass
IHTMLElementCollection collection = doc.body.all;
foreach (var htmlElem in collection)
{
if (htmlElem is mshtml.IHTMLDivElement)
{
mshtml.IHTMLDivElement div = htmlElem as mshtml.IHTMLDivElement;
if (div != null)
{
// HTMLDivElementClass divClass = (HTMLDivElementClass)div; ?????????
}
}
}我需要访问HTMLDivElementClass才能获得它的所有成员。

发布于 2012-03-07 05:38:31
你的代码几乎正确。不知道你想要什么。
请按以下方式更改代码
mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.body.all;
foreach (var htmlElem in collection)
{
if (htmlElem is mshtml.IHTMLDivElement)
{
mshtml.HTMLDivElementClass div = htmlElem as mshtml.HTMLDivElementClass;
if (div != null)
{
//DO YOUR CODE HERE
//div.IHTMLElement_id
}
}
}它为我工作,在"div“对象中,类型为"HTMLDivElementClass”。
还有一个建议,如果您只想从页面中获得所有的DIV标记,那么使用下面的代码行。
mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.getElementsByName("div");而不是
mshtml.IHTMLElementCollection collection = (mshtml.IHTMLElementCollection)objDocument.body.all;这将删除您的条件,以检查元素是否为DIV。
希望这对你有帮助。
https://stackoverflow.com/questions/9230284
复制相似问题