使用 webBrowser 控件来模拟点击网页上的链接,调用了INvokeMoember(click)方法,但好像不能在webbrowser浏览器打开。
为什么调用InvokeMember 程序直接调用了IE打开了呢
贴上代码希望管理员帮助一下
[C#] 纯文本查看 复制代码 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;
using System.Threading;
namespace WebBrowser_控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
if (!url.Equals(string.Empty))
{
webBrowser1.Navigate(url);
}
else
{
MessageBox.Show("网址不为空!","输入错误提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void DisplayMetaDescription()
{
if (webBrowser1.Document != null)
{
HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement elem in elems)
{
String nameStr = elem.GetAttribute("href");
if (nameStr != null && nameStr.Length != 0)
{
String contentStr = elem.GetAttribute("content");
MessageBox.Show("Document: " + webBrowser1.Url.ToString() + "\nDescription: " + contentStr);
}
}
}
}
/// <summary>
/// 查询页面链接地址,调用点击!
/// </summary>
/// <param name="url"></param>
private void herfclick(string url)
{
//for (int i = 0; i < webBrowser1.Document.All.Count; i++)
//{
// if (webBrowser1.Document.All[i].TagName == "a" && webBrowser1.Document.All[i].GetAttribute("href").ToString().Trim() == url)
// {
// webBrowser1.Document.All[i].InvokeMember("click");//引发”CLICK”事件
// break;
// }
//}
//获取一个HTML节点!
HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("a");
for (int i = 0; i < links.Count; i++)
{
if (links[i].GetAttribute("href").IndexOf("forum") > 0)
{
links[i].InvokeMember("click");
}
}
//遍历节点
//foreach (HtmlElement link in links)
//{
// //查找包含href节点
// string href = link.GetAttribute("href");
// if (href != null && href.Length != 0)
// {
// string u = link.GetAttribute("href");
// //过滤javascript链接,如果不包含就为真
// if (u.IndexOf("javascript")<0)
// {
// webBrowser1.Navigate(u);
// //Thread.Sleep(50000);
// }
// }
//}
}
private void button2_Click(object sender, EventArgs e)
{
//DisplayMetaDescription();
string name = textBox2.Text;
string pass = textBox3.Text;
if (zhuantian())
{
HtmlElement username = webBrowser1.Document.All["username"];
HtmlElement password = webBrowser1.Document.All["password"];
HtmlElement forms = webBrowser1.Document.Forms["lsform"];
HtmlElement btnAdd = webBrowser1.Document.GetElementById("addDiv").FirstChild;
username.SetAttribute("value",name);
password.SetAttribute("value",pass);
forms.InvokeMember("submit");
DisplayMetaDescription();
}
}
private bool zhuantian()
{
bool zhuan = true ;
return zhuan ;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
zhuantian();
}
}
private void button3_Click(object sender, EventArgs e)
{
string url = textBox1.Text;
if (zhuantian())
{
herfclick(url);
}
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
string url = this.webBrowser1.StatusText;
this.webBrowser1.Url = new Uri(url);
}
}
}
|