簡體   English   中英

如何將mshtml.IHTMLDivElement強制轉換為mshtml.HTMLDivElementClass?

[英]How to cast mshtml.IHTMLDivElement to mshtml.HTMLDivElementClass?

如何將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才能獲取它的所有成員。

在此處輸入圖片說明

您的代碼幾乎正確。 不知道你想要什么。

請像下面那樣更改您的代碼

 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的條件。

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM