惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
腾讯CDC
J
Java Code Geeks
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
雷峰网
雷峰网
B
Blog RSS Feed
博客园_首页
量子位
F
Fortinet All Blogs
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog

博客园 - Ring

运行Java cmd程序 找不到或无法加载主类怎么解决 Generics and Collection (2) Generics and Collection (1) mybatis, spring, springmvc 什么时候该用NoSQL? 如何学习Javascript 好文章尽收眼底 事务和锁 索引 VS调试Javascript. Asp.net常用状态管理方案分析 .NET中的缓存知识总结 "The server has rejected the client credentials"解决方案 - Ring 有关IIS7概述以及配置和开发的几篇文章摘录 是什么使得我的进程崩溃了? OutOfMemory这家伙是谁?为什么我还留有许多内存,它却使进程崩溃了? ASP.NET 崩溃-SiteMap中疯狂的循环 何时.NET中AppDomain会回收? .NET如何使用内存---餐馆案例分析
CodeForFun--编写自动登录Email的程序
Ring · 2006-11-09 · via 博客园 - Ring

每天早上一开机就登录博客园已经成了习惯, 总要先打开浏览器, 然后输入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模型,通过构建页面元素进行操作。