C#dC# 简单网页外挂实例

1 实例1

 此处所说的网页外挂即是网页自动化操作;获取网页上的元素属性;用程序点击网页上的按钮或链接等;

打开百度首页,在输入框自动输入10个字符串,自动点击 百度一下 按钮;

 

主要使用webbrowser控件操作;

首先用开发人员工具查找到百度首页的搜索输入框的id为kw,百度一下 按钮的id为su,如下图;

C#dC# 简单网页外挂实例_第1张图片

 

代码;

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 WindowsFormsApplication1
{
    
    public partial class Form1 : Form
    {
        string[] num=new string[10];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            num[0] = "13900020001";
            num[1] = "13900020002";
            num[2] = "13900020003";
            num[3] = "13900020004";
            num[4] = "13900020005";
            num[5] = "13900020006";
            num[6] = "13900020007";
            num[7] = "13900020008";
            num[8] = "13900020009";
            num[9] = "13900030001";

            webBrowser1.Navigate("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=123&rsv_pq=a164ac8e001fa18f&rsv_t=5928ojwGiyU/W+HC+1SXAAH+SMrz+p7+3sDiqQ90LpGYWNeI7K9mFhlixCo&rqlang=cn&rsv_enter=0&rsv_sug3=4&rsv_sug1=3&rsv_sug7=100&inputT=48720&rsv_sug4=48720");


        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                webBrowser1.Document.GetElementById("kw").SetAttribute("value", num[i]);
                webBrowser1.Document.GetElementById("su").InvokeMember("click");
            }
        }
    }
}


 

webBrowser1.Navigate 导航到某个网址;

设置属性;
webBrowser1.Document.GetElementById("kw").SetAttribute("value", num[i]);

调用方法;

webBrowser1.Document.GetElementById("su").InvokeMember("click");

 

运行情况;

C#dC# 简单网页外挂实例_第2张图片

你可能感兴趣的:(原创教程,.Net)