代码如下:
HtmlDocument html = webbrowser1.Document;
FindHelper.FindControlByAttValue("a", "class", "W_btn_d", "登录", html.All)
public static HtmlElement FindControlByAttValue(string Tag, string AttName, string AttValue, string innerText, HtmlElementCollection listOfHtmlControls)
{
try
{
int aa = listOfHtmlControls.Count;
foreach (HtmlElement element in listOfHtmlControls)
{
string str = element.OuterHtml;
if (!string.IsNullOrEmpty(element.OuterHtml))
{
string tagName = element.TagName;
if (element.TagName.ToLower() == Tag.ToLower())
{
string OuterHtml = element.OuterHtml;
if (OuterHtml == null) continue;
OuterHtml = OuterHtml.Substring(0, OuterHtml.IndexOf(">"));
OuterHtml = OuterHtml.ToLower();
if (OuterHtml.Contains(AttName.ToLower()) && OuterHtml.Contains(AttValue.ToLower()))
{
string e = element.InnerText;
if (e == null) e = "";
if (e.Trim().ToLower().Contains(innerText.ToLower()))
{
return element;
}
}
}
}
}
}
程序刚开始没问题,运行一段时间会报这个错,格式转换的不对?要怎么做?
报错:
Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to interface type 'IHTMLDocument2'.
(Exception from HRESULT: 0x80028019 (TYPE_E_UNSUPFORMAT)).
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, Boolean& pfNeedsRelease)
at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetAll()
at System.Windows.Forms.HtmlDocument.get_All()