zoukankan      html  css  js  c++  java
  • C# webbrowser全掌握(二)

    全篇引用单元mshtml;

          路径:C:windowsassemblyGACMicrosoft.mshtml7.0.3300.0__b03f5f7f11d50a3aMicrosoft.mshtml.dll  //不同的版本路径不同


    一、如何用Webbrowser获取网页的全部源代码

           1.不含框架 

             string s= WB1.DocumentText;

           2.含有框架

            IHTMLDocument2 doc=WB1.Document.DomDocument as IHTMLDocument2;
            IHTMLFramesCollection2 frames=doc.frames as IHTMLFramesCollection2;
            int i_frame=0;  //第1个框架
            IHTMLWindow2 frame=frames.item(ref i_frame) as IHTMLWindow2;
            IHTMLDocument2 doc2=frame.document as IHTMLDocument2;
            IHTMLDocument3 doc3=frame.document as IHTMLDocument3; 

            strings = doc2.body.innerHTML;

    二、webbrowser如何获取网页的元素

           1.根据元素ID

            HtmlElement ele= WB1.Document.GetElementById("ID");


            2.根据元素Name

             HtmlElement ele= WB1.Document.All["Name"];

            3. 根据元素的索引序号

                HtmlElement ele=WB1.Document.All[0];  //第1个元素

                HtmlElement ele=  WB1.Document.GetElementsByTagName("input")[0];  //第一个inuput类型的元素



            4.无ID无Name的元素

               4.1  webbrowser遍历网页所有元素:

           HtmlDocument doc=WB1.Document;

          HtmlElementCollection elements=doc.All;

          foreach(HtmlElement element in elements)

          {

            if (element.GetAttribute("innerText") == "提交回答") //元素的innnerText属性为“提交回答”

            {

                element.InvokeMember("Click");  //模拟点击

                break;

            }

          }  

              4.2 webbrowser   根据元素的Tag遍历(有HTML、Form、Table、TR、TD、Div、A、 IMG、Li、Input、Span等

               input: 所有可输入的类型,例如文本框、勾选框、复选框、下拉列表、图片等

               a:          A标签,可以带超链接

               img:   图片类型

              span:    SPAN类型

              li:           下拉列表,列表框等

             div:         DIV      


               HtmlElementCollection  eles = WB1.Document.GetElementsByTagName("a") as HtmlElementCollection;  //所有的A标签集合
                foreach (HtmlElement ele in eles)
                {
                    if (ele.GetAttribute("href") != null)
                    if (ele.GetAttribute("href") == "A标签的超链接") 
                    {                    
                        element.InvokeMember("Click");  //模拟点击
                        break;
                    }
                }    

            4.3 根据已知元素获取未知元素

    例如:  

    获取已知ID的下一个节点

      HtmlElement ele=   WB1.Document.GetElementById("元素的ID") .NextSibling; //上个节点 previousSibling

    获取已知ID的父节点的第1个节点

      HtmlElement ele=   WB1.Document.GetElementById("元素的ID") .Parent.Children[0]; //或者firstChild

     三、webbrowser模拟填表


    四、webbrowser执行JS函数

       1.用Navigate

                 WB1.Navigate("javascript:alert('弹出信息!');");    //alert为要执行的JS函数
                 WB1.Navigate("javascript:postComment();");    //postComment为要执行的JS函数

        2.用IhtmlWindow2接口

                IHTMLWindow2 win2 = WB1.Document.Window.DomWindow as IHTMLWindow2;
                win2.execScript("function confirm(){return true;}", "javascript");

       3.用IhtmlDocument2接口的parentWindow
                IHTMLDocument2 doc2 = WB1.Document.DomDocument as IHTMLDocument2;
                doc2.parentWindow.execScript("function confirm() {return true;}", "javascript");



    五、Webbrowser控制始终在本窗口打开
       在NewWindow事件写代码:
           private void WB1_NewWindow(object sender,CancelEventArgs e)
        {
          e.Cancel=true;   //屏蔽弹出到IE浏览器
          // ppDisp=WB2.ActiveXInstance;  //新建的webbrowser(WB2)窗口打开;

          string url=WB1.Document.ActiveElement.GetAttribute("href");
          if(url=="")
              url= WB1.StatusText;  //获取URL方法二
          WB1.Navigate(url);  //本窗口打开
        }

    六、其他
       1.控制其放大缩小
         WB1.Document.Body.Style="zoom:50%";   //缩小50%
       2.让webbrowser的内容适应webbrowser的大小
          Size szb=new Size(WB1.Document.Body.OffsetRectangle.Width,WB1.Document.Body.OffsetRectangle.Height);
          Size sz=WB1.Size;
          int xbili=(int)((float)sz.Width/(float)szb.Width*100);//水平方向缩小比例
          int ybili=(int)((float)sz.Height/(float)szb.Height*100);//垂直方向缩小比例
          WB1.Document.Body.Style="zoom:"+xbili.ToString()+"%";
          WB1.Invalidate();
    3.获取滚动条的位置:
        HtmlDocument document = WB1.Document;

        int top = document.GetElementsByTagName("HTML")[0].ScrollTop;//滚动条垂直位置

       指定滚动条滚动到指定位置

       WB1.Document.Window.ScrollTo(0, 100);//滚动到100的位置

       WB1.Document.Window.ScrollTo(0, WB1.Document.Body.ScrollRectangle.Height);//滚动到底部

    七、Webbrowser遍历网页元素

    //不引用其他单元
                   foreach(HtmlElement ele in WB1.Document.All)
                    {
                      if(ele.InnerText=="下一页>")
                      {
                        ele.InvokeMember("Click");
                        break;
                      }
                    }
    
    //引用mshtml;
                     IHTMLDocument2 doc = WB1.Document.DomDocument as IHTMLDocument2;
                    foreach (IHTMLElement ele in doc2.all)
                    {
                        if (ele.innerText == "下一页>")
                        {
                            ele.click();
                            break;
                        }
                    }


  • 相关阅读:
    DELPHI SOKET 编程(使用TServerSocket和TClientSocket)
    DELPHI 任务栏无EXE显示
    Delphi 实现无窗口移动(详细使用WM_NCHITTEST和PtInRect API进行测试)
    ViewPager的简单使用
    delphi 网页提交按钮执行点击事件
    Delphi 获取网站验证码的图片
    Delphi 模拟网站验证码(酷,把随机文字写道图片上)
    张文木的文章都很不错,有空仔细看看
    深度RAMOS,把操作系统全部安装在内存上
    C# ASP.net 入门之简单通讯录
  • 原文地址:https://www.cnblogs.com/xtfnpgy/p/9285418.html
Copyright © 2011-2022 走看看