• 模拟操作网页 webBrowser


    C# 获取IFrame中body元素 (winform)

    方法1.

    找出iframe的b.html的src , 利用webbrowser去加载b.html


    HtmlElementCollection a = webBrowser1.Document.All["loginframe"].GetElementsByTagName("iframe");
    foreach (HtmlElement kk in a)
      {
         textBox1.Text = kk.GetAttribute("src");
    }
    或者

    webBrowser1.Document.GetElementById("iframe的id").GetAttribute("src");

    webBrowser1.Navigate("src");

    方法2.

    使用 frame的 document

    webBrowser1.Document.Window.Frames["login_ifr"].Document;

    方法只是抛砖引玉,大家自己去专研去把  转自:http://hi.baidu.com/dingding3224/item/8060946dac3de5166895e6d4

     HtmlDocument doc = webBrowser1.Document.Window.Frames["centerframe"].Document;
                    doc.GetElementById("code").InnerText = "361100210009480";
                    HtmlElement he = null;
                    he = doc.GetElementById("btnSearch");
                    he.InvokeMember("Click");

     在使用Webbrowser时,经常会遇到网页没有加载完成就进一步进行操作,这样不但取不到控件或数据,而且会报错,这里在网上找两个一个函数,对Webbrowser加载进行等待,加载好后再进行处理:

    private void Delay(int Millisecond) //延迟系统时间,但系统又能同时能执行其它任务;
            {
                DateTime current = DateTime.Now;
                while (current.AddMilliseconds(Millisecond) > DateTime.Now)
                {
                    Application.DoEvents();//转让控制权            
                }
                return;
            }
            private bool WaitWebPageLoad(WebBrowser WEB)
            {
                int i = 0;
                string sUrl;
                while (true)
                {
                    Delay(500);  //系统延迟50毫秒,够少了吧!             
                    if (WEB.ReadyState == WebBrowserReadyState.Complete) //先判断是否发生完成事件。
                    {
                        if (!WEB.IsBusy) //再判断是浏览器是否繁忙                  
                        {
                            i = i + 1;
                            if (i == 2)   //为什么 是2呢?因为每次加载frame完成时就会置IsBusy为false,未完成就就置IsBusy为false,你想一想,加载一次,然后再一次,再一次...... 最后一次.......
                            {
                                sUrl = WEB.Url.ToString();
                                if (sUrl.Contains("res")) //这是判断没有网络的情况下                           
                                {
                                    return false;
                                }
                                else
                                {
                                    return true;
                                }
                            }
                            continue;
                        }
                        i = 0;
                    }
                }
            }

    下面是处理网页元素的代码示例:

     private void GetSearchPage()
            {
                try
                {
                    WebBrowser mainwb = new WebBrowser();
                    Navigate(mainwb, strAddress);
                    //加载完后获取查询页
                    WaitWebPageLoad(mainwb);
    
                    HtmlDocument doc = mainwb.Document;
                    HtmlElement he = null;
                    he = doc.GetElementById("da");
                    he.InvokeMember("Click");
                    //输入数据进行查询
                    WaitWebPageLoad(mainwb);
                    HtmlDocument doc3 = mainwb.Document.Window.Frames["centerframe"].Document;
                    doc3.GetElementById("code").InnerText = "361100210009480";
                    //doc.GetElementById("j_password").InnerText = textBox2.Text;
                    //doc.GetElementById("j_captcha").InnerText = textBox3.Text;
                    HtmlElement he3 = null;
                    he3 = doc3.GetElementById("btnSearch");
                    he3.InvokeMember("Click");
                    //获取查询出来的超链接
                    WaitWebPageLoad(mainwb);
                    HtmlDocument doc1 = mainwb.Document.Window.Frames["centerframe"].Document;
    
                    HtmlElementCollection hec = doc1.GetElementsByTagName("TD");
                    ArrayList arrHref = new ArrayList();
                    arrHref.Clear();
                    //string a = "";
                    for (int i = 0; i < hec.Count; i++)
                    {
                        if (hec[i].InnerHtml != null)
                        {
                            //hec[i].InvokeMember("Click");
                            if (hec[i].InnerHtml.ToString().Contains("href"))
                            {
                                string href = hec[i].InnerHtml.ToString();
    
                                arrHref.Add(href.Substring(href.IndexOf("href") + 6, href.LastIndexOf("">") - 6 - href.IndexOf("href")));
    
                            }
                        }
                    }
                    foreach (string href in arrHref)
                    {
                        WebBrowser Datawb = new WebBrowser();
                        Navigate(Datawb, strAddress + href);
                        WaitWebPageLoad(Datawb);
                        HtmlDocument doca = null;
                        doca = Datawb.Document;
                        string b = doca.GetElementById("txtITEMcode").InnerText;
                        arrReturn.Add(b);
                        //GetOneRecord();
                        //webBrowser2.Navigate(Str_Websit);
                        //Datawb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetOneRecord);
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
  • 相关阅读:
    Go
    go-反射
    go-map
    go中数组与切片
    goroutine 和 channel
    Go中的闭包
    新版 C# 高效率编程指南
    gitignore 规则和不起作用的解决方案
    Linux系统部署.Net Core3.1项目
    List<对象> 根据某个字段提出一个 List<String>,并且去重
  • 原文地址:https://www.cnblogs.com/tylertang/p/3256186.html
Copyright © 2020-2023  润新知