C#控制IE浏览器

项目-添加引用-COM,添加下面两个:

Microsoft Internet Controls
Microsoft HTML Object Library

private void button1_Click(object sender, EventArgs e)
{
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.DocumentComplete += ie_DocumentComplete;
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_DocumentComplete(object pDisp, ref object URL) //加载完成事件
{
ie_read = true;
}
private void compWait() //等待,直到加载完成
{
while (ie_read != true)
{
Application.DoEvents();
}
}

如果网页中没有设置id,只能通过name查找

mshtml.IHTMLElementCollection userids = doc.getElementsByName("userid");
foreach (mshtml.IHTMLElement userid in userids)
{
userid.innerText = "USERID";
}

控制已经打开的IE浏览器:

public static SHDocVw.InternetExplorer getInternetExploer(string url)
{
var shell = new Shell32.Shell();
var windows = (SHDocVw.IShellWindows)shell.Windows();
SHDocVw.InternetExplorer ie;
foreach (object window in windows)
{
ie = window as SHDocVw.InternetExplorer;
if (ie != null &&
string.Equals(System.IO.Path.GetFileName(ie.FullName),
"iexplore.exe", StringComparison.CurrentCultureIgnoreCase))
{
if (ie.LocationURL == url)
{
return ie;
}
}
}
return null;
}

Frame的控制

mshtml.HTMLDocument doc2 = ie.Document;
var frame = doc2.frames.item(0);
var doc = frame.Document;
doc.getElementById...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值