本人新手,想做个网页数据的监控,但需要用webkit内核才能正常访问网页 ,用了几个方式,都处理不了查找不到数据时返回错误:对 COM 组件的调用返回了错误 HRESULT EFAIL
用VS自带WebBrowser是可以判断,用webkit一直错误
测试的代码如下,求各路大神指教!
Public void WebTest()
{
while (true)
{
int i = 0;
i++;
try
{
notifyIcon1.ShowBalloonTip(500, "线程测试!", i.ToString(), ToolTipIcon.Info);
//刷新
button2.Invoke(newEventHandler(delegate
{
webKitBrowser1.Reload();
}
));
Thread.Sleep(5000);
//1
if (this.IsHandleCreated)
{
webKitBrowser1.Invoke(newEventHandler(delegate
{
WebKit.DOM.Element inputLoginName = (WebKit.DOM.Element)webKitBrowser1.Document.GetElementById("LoginName");
MessageBox.Show(inputLoginName.TagName);
//2
string par1 = webKitBrowser1.StringByEvaluatingJavaScriptFromString("document.getElementById('LoginName').TagName");
MessageBox.Show(par1);
//3
WebKit.DOM.Document htmlDoc = webKitBrowser1.Document;
WebKit.DOM.Element btnElement = htmlDoc.GetElementById("LoginName");
Console.Write(btnElement);
if (btnElement == null)
{
MessageBox.Show("空值!!!");
}
//// 错误:对 COM 组件的调用返回了错误 HRESULT E_FAIL
//4
string check = webKitBrowser1.Document.GetElementById("LoginName").TagName;
if(string.IsNullOrEmpty(check))
{
MessageBox.Show("空值测试!!!");
}
if (check == "INPUT")
{
MessageBox.Show("请登录后重试!", "未登录");
t.Abort();
}
}
));
}
}
catch (Exception ex)
{
if (ex.Message != "正在中止线程。")
{
MessageBox.Show(ex.Message, "异常信息");
break;
}
}
}
}