四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2308|回复: 0
打印 上一主题 下一主题

WebBrowser网页操作之提取获取元素和标签

[复制链接]
  • TA的每日心情
    开心
    2018-12-28 16:25
  • 817

    主题

    1556

    帖子

    1万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    14941
    跳转到指定楼层
    楼主
    发表于 2016-10-26 21:44:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    http://www.cnblogs.com/isaced/archive/2011/01/24/1943227.html
    在此基础上加强

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;//

    namespace WebBrowser网页操作
    {
        public class Element
        {

            //根据Name获取元素
            public HtmlElement GetElement_Name(WebBrowser wb,string Name)
            {
                HtmlElement e = wb.Document.All[Name];
                return e;
            }

            //根据Id获取元素
            public HtmlElement GetElement_Id(WebBrowser wb, string id)
            {
                HtmlElement e = wb.document.getElementByIdx(id);
                return e;
            }

            //根据Index获取元素
            public HtmlElement GetElement_Index(WebBrowser wb,int index)
            {
                HtmlElement e = wb.Document.All[index];
                return e;           
            }

      // 据Type获取元 ,在没有NAME和ID的情况下使用   
      public HtmlElement GetElement_Type(WebBrowser wb,string type)   
      {        
       HtmlElement e=null;        
       HtmlElementCollection elements = wb.Document.getElementsByTagName_r("input");        
       foreach (HtmlElement element in elements)        
       {            
        if (element.GetAttribute("type") == type)            
        {               
         e = element;           
        }        
       }        
       return e;   
      }
      // 据Type获取元 ,在没有NAME和ID的情况下使用,并指定是同类type的第 个,GetElement_Type()升级版   
      public HtmlElement GetElement_Type_No(WebBrowser wb,string type,int i )   
      {
       int j=1;        
       HtmlElement e=null;        
       HtmlElementCollection elements = wb.Document.getElementsByTagName_r("input");        
       foreach (HtmlElement element in elements)        
       {            
        if (element.GetAttribute("type") == type)            
        {               
         if(j==i)
         {
          e = element;
         }
         j++;            
        }        
       }        
       return e;   
      }
           //获取form表单名name,返回表单
      public HtmlElement GetElement_Form(WebBrowser wb,string form_name)
      {
       HtmlElement e = wb.Document.Forms[form_name];
       return e;
      }
      //设置元素value属性的值
      public void Write_value(HtmlElement e,string value)
      {
       e.SetAttribute("value", value);
      }
             
      //执行元素的方法,如:click,submit(需Form表单名)等
      public void Btn_click(HtmlElement e,string s)
      {
       e.InvokeMember(s);
      }
        }
    }
    这是调用这个类的窗体代码:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WebBrowser网页操作
    {
        public partial class Form1 : Form
        {
            Element el = new Element();

            public Form1()
            {
                InitializeComponent();
            }
             
            private void Form1_Load(object sender, EventArgs e)
            {
                webBrowser1.Navigate(Application.StartupPath + @"\Test.html");
            }

            private void button1_Click(object sender, EventArgs e)
            {
                el.Write_value(el.GetElement_Name(webBrowser1,"username"),"isaced");
            }

            private void button2_Click(object sender, EventArgs e)
            {
                el.Write_value(el.GetElement_Id(webBrowser1, "password"), "123456");
            }

            private void button3_Click(object sender, EventArgs e)
            {
                el.Btn_click(el.GetElement_Id(webBrowser1,"button"),"click");//方法用的按钮click
            }

            private void button4_Click(object sender, EventArgs e)
            {
                el.Btn_click(el.GetElement_Form(webBrowser1, "form1"), "submit");//先获取表单,再调用表单的submit方法
            }
        }
    }

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-6 07:52 , Processed in 1.093750 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表