C# webbrowser 获取网页元素 示例代码

在应用webbrowser对网页进行自动化操作时,不能有效地获取网页元素,往往是后续编程的拦路虎,原因是webbrowser提供的GetElementById()、GetElementFromPoint()、GetElementsByTagName()共3个方法,如果不仔细的研究,使用起来还真的是有些障碍。为了解决这些问题,我只要硬着头皮,费劲心思地研究了一番。至于说有什么成果没有,那就只有本文的读者自己体会了。下面用图片给大家展示一下,同时,把代码亮出来分享,以对大家有些小小的帮助。

网页元素要想看清楚些,用到的工具就是《开发人员工具》,看到了吧,浏览器都是自带的。

点击工具左上方的鼠标状按钮可以精确定位网页元素。

  程序运行效果

  1. 加载网页

 2、用ID查找搜索框,搜索框为有ID的input标签,显示对话框内容为该标签的类型

 聚焦到搜索框并填写“传奇霸主”字符串

 用标签内文本(innertext)查找标签,对话框框显示标签类型为超链接a。

 

 随后自动点击“全部游戏”超链接

  1. 查找iframe窗口中的元素方法。遗憾的是这里并没有写成单独的方法,而是直接放在了响应按钮的事件方法中。这主要是因为这里基本上是手工完成,如果要想遍历层次结构的iframe大概需要写一个递归的方法,可惜咱么太业余,完不成这个艰巨的任务。

首先是获得iframe窗口的句柄,然后再通过name属性查找输入框,最后写入内容

4、这个方法首先是做了一个确定元素在web中相对坐标的方法,然后根据显示内容,手工填写元素相对坐标在要编译的程序变量里。然后通过密码框在iframe中的相对位置获得了句柄,并写入密码。

 

下面是程序的源代码

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 wg5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //用id查找元素
        private HtmlElement get_elem_by_id(WebBrowser w, string id) {
            return w.Document.GetElementById(id);
        }

        //用name属性查找元素,这个没有用到,因为是在iframe中查找name属性,所以手工做了
        private HtmlElement get_elem_by_name(WebBrowser w, string name) {
            return w.Document.All[name];
        }

        //用元素标签中的内容查找元素
        private HtmlElement get_elem_by_innertext(WebBrowser w, string innertext) {
            HtmlElement e = null;
            StringBuilder s = new StringBuilder();
            foreach (HtmlElement t in w.Document.All) {
                s.Clear();
                s.Append(t.InnerText);
                if (s.ToString().Equals(innertext)) {
                    e = t;
                    break;
                }
            }
            return e;
        }

        //查找元素的方法还有用type属性查找,用元素集合(bytagname)查找等,以上都是参考网上大咖,但觉得效果一般,因此没有列出

        private void button1_Click(object sender, EventArgs e)
        {
            //加载url,网页显示在web容器中
            webBrowser1.Navigate(textBox1.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //获取鼠标在web容器的中的相对坐标
            // 第一个为鼠标相对屏幕的坐标,第二个为本程序窗口的坐标(窗口移动不影响计算)
            //第三个常量为预估的窗口边框的横纵向长度,第四个是web相对于窗口内边的距离(窗口设计时,已经设置为0)
            int x = Form1.MousePosition.X - this.Location.X - 8 - webBrowser1.Location.X;
            int y = Form1.MousePosition.Y - this.Location.Y - 30 - webBrowser1.Location.Y;
            label3.Text = x.ToString();
            label5.Text = y.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HtmlElement he = get_elem_by_id(webBrowser1, "search-input");
            if (he != null)
            {
                MessageBox.Show(he.TagName);
                he.Focus();
                he.SetAttribute("value", "传奇霸主");
            }
            else {
                MessageBox.Show("没有找到元素句柄!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            HtmlElement he = get_elem_by_innertext(webBrowser1, "全部游戏");
            if (he != null)
            {
                MessageBox.Show(he.TagName);
                he.InvokeMember("click");
            }
            else
            {
                MessageBox.Show("没有找到元素句柄!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            HtmlElement he = null;
            label6.Text = webBrowser1.Document.Window.Frames.Count.ToString();
            HtmlWindow hw = webBrowser1.Document.Window.Frames[1];
            he = hw.Document.All["userName"];
            if (he != null)
            {
                MessageBox.Show(he.TagName);
                he.SetAttribute("value", "ideal");
            }
            else
            {
                MessageBox.Show("没有找到元素句柄!");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Point p = new Point();
            //这个数据是用鼠标手工测算的输入框(密码)相对于iframe窗口的距离,不用精确,鼠标落在元素上预估即可
            p.X = 77 - 18; 
            p.Y = 287 - 172;
            HtmlWindow hw = webBrowser1.Document.Window.Frames[1];
            HtmlElement he = hw.Document.GetElementFromPoint(p);
            if (he != null)
            {
                MessageBox.Show(he.TagName);
                he.SetAttribute("value", "123456");
            }
            else
            {
                MessageBox.Show("没有找到元素句柄!");
            }

        }
    }
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值