频道栏目
首页 > 资讯 > C# > 正文

C#控制IE打开网页,输入用户名、密码并登录的操作代码实例

18-07-19        来源:[db:作者]  
收藏   我要投稿

注意,要添加两个COM引用

Microsoft HTML Object Library

Microsoft Internet Controls

BUTTON1打开百度查询

BUTTON2模拟输入用户密码,并登录。

private void button1_Click(object sender, EventArgs e)
  {
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.DocumentComplete += ie_DocomentComplete; 
ie.Navigate("http://www.baidu.com");
ie.Visible = true;
compWait(); 
mshtml.HTMLDocument doc = ie.Document;
doc.getElementById("kw").innerText = "中国";
doc.getElementById("su").click(); 
  }

  private bool ie_read = false;
  private void ie_DocomentComplete(object pDisp, ref object URL) //加载完成事件
  {
ie_read = true;
  }

  private void compWait() //等待,直到加载完成
  {
while (ie_read != true)
{
 Application.DoEvents();
}
  }

  private void button2_Click(object sender, EventArgs e)
  {
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.DocumentComplete += ie_DocomentComplete;
ie.Navigate("http://10.100.200.300/XXXXX/XXXXX.do");
ie.Visible = true;
compWait();
mshtml.HTMLDocument doc = ie.Document;
mshtml.IHTMLElementCollection userids = doc.getElementsByName("userid");
foreach (mshtml.IHTMLElement userid in userids)
{
 userid.innerText = "xiaoming";
}
mshtml.IHTMLElementCollection passwords = doc.getElementsByName("password");
foreach (mshtml.IHTMLElement password in passwords)
{
 password.innerText = "888888";
}
mshtml.IHTMLElementCollection syaincds = doc.getElementsByName("syaincd");
foreach (mshtml.IHTMLElement syaincd in syaincds)
{
 syaincd.innerText = "0";
}
mshtml.IHTMLElementCollection logins = doc.getElementsByName("login");
foreach (mshtml.IHTMLElement login in logins)
{
 login.click();
}

System.Threading.Thread.Sleep(3000);
mshtml.IHTMLElementCollection tencds = doc.getElementsByName("login");
foreach (mshtml.IHTMLElement tencd in tencds)
{
 tencd.click();
}
  }
相关TAG标签
上一篇:Android开发之ListView功能扩展,实现高性能的瀑布流布局讲解
下一篇:python中的各种语句实例讲解
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站