我在做一个Web网页自动测试实施工具。用的是SHDocVw.InternetExplorer。
但是不能访问InternetExplorer.Document.parentWindow。
我把代码简化到最简。哪位朋友会能否帮下忙?谢谢!(就是下面的Exception System.InvalidCastException)
using System;
using System.Collections.Generic;
using SHDocVw;
using System.Threading;
using System.Drawing;
using System.Runtime.InteropServices;
using MSHTML;
namespace TestProject
{ class Class1
{ AutoResetEvent documentComplete = new AutoResetEvent(false);
public static void Main(string[] args)
{ new Class1();
}
public Class1()
{ SHDocVw.InternetExplorer ie=new InternetExplorerClass();
ie.DocumentComplete+=new DWebBrowserEvents2_DocumentCompleteEventHandler(WhenDocumentComplete);
ie.Visible=true;
string url="www.baidu.com";
object ept=System.Type.Missing;
ie.Navigate(url,ref ept,ref ept,ref ept,ref ept);
documentComplete.WaitOne();
IHTMLDocument2 d=(IHTMLDocument2)ie.Document;
System.Console.Write(d.readyState);//输出为Complete
d.parentWindow.execScript(@"document.getElementByName('wd').value='OK';","javascript");//Exception System.InvalidCastException was thrown in debuggee: Specified cast is not valid.这里的意思是d.parentWindow访问出错。不知道为什么
}
public void WhenDocumentComplete(object p1,ref object p2)
{ documentComplete.Set();
}
}
}