通过c#获取iframe文档对象

时间:2012-10-05 12:31:38

标签: c# multithreading iframe domdocument mshtml

我有这个代码片段需要检索iframe的文档对象并将其保存在对象中。

//just a wrapper object to store the document object   
DocObj obj = new DocObj(HtmlDoc);

ManualResetEvent aDoneEvents = new ManualResetEvent(false);
string errorMessage = "";
Thread aThread = new Thread(() =>
{
    try
    {
        //finding the iframe element in the current document
        IHTMLElement IElem = LocatorBuilder.GetLocator(Target)
                   .Find(this.HTMLDoc);

        obj.HTMLDoc = (IElem as HTMLIFrame).document as HTMLDocument;

    }
    catch (Exception e)
    {
        errorMessage = e.Message;
    }
    aDoneEvents.Set();
});

aThread.SetApartmentState(ApartmentState.STA);
aThread.IsBackground = true;
aThread.Start();
aDoneEvents.WaitOne();
if (errorMessage != "")
{
    throw new Exception(errorMessage);
}
HtmlDoc = obj.HTMLDoc;

但是当我尝试检索新文档对象时,我在最后一行得到了这个例外:

  

由于先前的功能评估,功能评估被禁用   时间到。您必须继续执行以重新启用功能评估

1 个答案:

答案 0 :(得分:0)

mshtml.IHTMLDocument3 doc3 =   (mshtml.IHTMLDocument3)webBrowser.Document.DomDocument;
mshtml.IHTMLFrameBase2 frame = (mshtml.IHTMLFrameBase2)doc3.getElementById("iframe_name");
if (frame != null)
{
    mshtml.IHTMLWindow2 w2 = (mshtml.IHTMLWindow2)frame.contentWindow;
    doc3 = (mshtml.IHTMLDocument3)w2.document;
}