well_007 2017-08-12 09:24 采纳率: 0%
浏览 1220

求助大神 C# webKit 查找网页元素

本人新手,想做个网页数据的监控,但需要用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;
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2017-08-12 15:28
    关注

    webKitBrowser这个东西你用了就有体会了,是一个半拉子工程,bug特别多。

    评论

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决