• CodeForFun编写自动登录Email的程序


    每天早上一开机就登录博客园已经成了习惯, 总要先打开浏览器, 然后输入www.cnblogs.com,然后等待... ...
    要是某天有火箭队的比赛, 还要去关注一下赛事情况: 先打开浏览器, 然后输入www.sohu.com,点NBA,然后... ...
    哈哈, 还有一件少不了的事情,就是要登录电子油箱查看一下是否有新邮件...

    这么多操作真是麻烦! 能不能通过点击一个按纽来简化这些操作呢?

    前两天从同事(ZnS04)那里得到启示, 根据自动化测试的原理, 完全可以...

    好了,下面就开始编写吧 ... ...

    1).  新建一个 window application 工程.
    2). 在窗体上添加3个Button,设置好属性, 如图:


    3). 给工程添加2个引用 Microsoft.mshtml 和SHDocVw.dll
    4). 编写代码

     1using System;
     2using System.Collections.Generic;
     3using System.ComponentModel;
     4using System.Data;
     5using System.Drawing;
     6using System.Text;
     7using System.Windows.Forms;
     8using System.Threading;
     9//添加引用
    10using SHDocVw;
    11using mshtml;
    12
    13namespace WebExplorer
    14{
    15    public partial class Form1 : Form
    16    {
    17        public Form1()
    18        {
    19            InitializeComponent();
    20        }

    21
    22        /// <summary>
    23        /// Redirect to the URL page.
    24        /// </summary>
    25        /// <param name="URL">Your wanted URL.</param>

    26        public void GotoURL(string URL)
    27        {
    28            //实例化一个IE模型
    29            SHDocVw.InternetExplorer IE = new InternetExplorer();            
    30            IE.Visible = true;
    31            object nullArg = null;
    32            //引导到URL
    33            IE.Navigate(URL, ref nullArg, ref nullArg, ref nullArg, ref nullArg);
    34        }

    35
    36        private void gotocnBlogs_Click(object sender, EventArgs e)
    37        {
    38            this.GotoURL("www.cnblogs.com");
    39        }

    40
    41        private void gotoNBA_Click(object sender, EventArgs e)
    42        {
    43            this.GotoURL("sports.sohu.com/nba"); 
    44        }

    45
    46        private void gotoGmail_Click(object sender, EventArgs e)
    47        {
    48            try
    49            {
    50                
                            this.GotoURL("http://gmail.google.com");
    55
    56                Thread.Sleep(3000);
    57                //得到IE的文档对象模型
    58                mshtml.IHTMLDocument2 DOM = (mshtml.IHTMLDocument2)IE.Document;
    59                //声明用户名
    60                mshtml.IHTMLInputTextElement txtUserName = (mshtml.IHTMLInputTextElement)DOM.all.item("Email"null);
    61                txtUserName.value = "YOUE USERNAME";
    62                //声明密码
    63                mshtml.IHTMLInputTextElement txtPwd = (mshtml.IHTMLInputTextElement)DOM.all.item("Passwd"null);
    64                txtPwd.value = "PASSWORD";
    65                //声明登录
    66                mshtml.HTMLInputElement btnLogin = (mshtml.HTMLInputElement)DOM.all.item("null"0);
    67                Thread.Sleep(1000);
    68                btnLogin.click();
    69            }

    70            catch (Exception ex)
    71            {
    72
    73            }

    74        }

    75    }

    76}


    5. 运行工程,点击上面的Button,OK!

    需要说明两点:
    1. 关于两个引用
       Microsoft.mshtml:这个引用可以从add reference->.NET中得到。
       SHDocVw.dll:这个引用在windows/system32目录下。
    2.对上述dll进行引用后,即可实例化IE模型,通过构建页面元素进行操作。
  • 相关阅读:
    春秋战国时期灭了三个国家的陈国女人
    学历史有什么用?
    真正的奴才韩非
    深度学习的历史
    深度学习三十年
    图算法
    几种常见的查找算法
    数据结构之基于堆的优先队列
    几种常见的排序算法
    数据结构(背包、队列和栈)
  • 原文地址:https://www.cnblogs.com/Ring1981/p/450744.html
Copyright © 2020-2023  润新知