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;
                    }
                }


原文地址:https://www.cnblogs.com/xtfnpgy/p/9285418.html