C#:在WebBrowser控件中阻止alert[警告框],confirm[确认对话框]对话框」及解析网页框架集\触发按钮事件
时间:2010-09-09 来源:且行且思

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mshtml;
namespace GoodClient
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.Text != "")
{
try
{
webBrowser1.Navigate(this.comboBox1.Text);
}
catch { }
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this.comboBox2.Text != "")
{
try
{
webBrowser2.Navigate(this.comboBox2.Text);
}
catch { }
}
}
/*在WinForm中设置要使用组合键的窗体的KeyPreview(向窗体注册键盘事件)属性为True;
然后使用窗体的KeyDown事件(在首次按下某个键时发生).
*/
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{
button1_Click(sender, e);
}
if (e.KeyCode == Keys.F6)
{
button2_Click(sender, e);
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.Text = http://aaaa.com/;
}
private void button3_Click(object sender, EventArgs e)
{
if (webBrowser1.Document.Window.Frames == null)
return;
HtmlDocument htmlDoc = webBrowser1.Document.DomDocument as HtmlDocument;
int row = 0;
//订单页
this.textBox1.Text = webBrowser1.Document.Window.Frames[0].Frames[1].Document.Body.InnerHtml;
// row = webBrowser1.Document.Window.Frames.Count;
//for (int i = 0; i < row; i++)
//{
// MessageBox.Show(webBrowser1.Document.Window.Frames[i].Document.Body.InnerHtml, row.ToString() + "A");
//}
////框架集
// row = webBrowser1.Document.Window.Frames[0].Frames.Count;
//for (int i = 0; i < row; i++)
//{
// //MessageBox.Show(webBrowser1.Document.Window.Frames[0].Frames[i].Document.Body.InnerHtml , i.ToString() + "B");
// //MessageBox.Show(webBrowser1.Document.Window.Frames[0].Frames[i].Name, i.ToString() + "Name");
//}
////左框架列表页
row = webBrowser1.Document.Window.Frames[0].Frames[2].Frames.Count;
for (int i = 0; i < row; i++)
{
// MessageBox.Show(webBrowser1.Document.Window.Frames[0].Frames[2].Frames[i].Document.Body.InnerHtml, i.ToString() + "T");
this.textBox2.Text = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[i].Document.Body.InnerHtml;
}
//htmlDoc = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[1].Document;
//System.Windows.Forms.HtmlElementCollection myLiks = htmlDoc.Links;
//foreach (HtmlElement link in myLiks)
//{
// this.textBox1.Text += link.GetAttribute("href") + "\r\n ";
//}
}
private void button4_Click(object sender, EventArgs e)
{
HtmlDocument htmlDoc = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[1].Document;
System.Windows.Forms.HtmlElementCollection myLiks = htmlDoc.Links;
string tmp_ads_url = "&type=C&gnum=30241&odd_f_type=H";
foreach (HtmlElement link in myLiks)
{
if (link.GetAttribute("href").Contains(tmp_ads_url))
{
link.InvokeMember("click");
break;
}
}
}
private void button6_Click(object sender, EventArgs e)
{
HtmlDocument htmlDoc = webBrowser1.Document.Window.Frames[0].Frames[1].Document;
foreach (HtmlElement h in htmlDoc.All)
{
if (h.GetAttribute("type").ToString() == "button")
{
if (h.GetAttribute("value").ToString() == "确定")
{
h.InvokeMember("onclick");
break;
}
}
}
}
private void button5_Click(object sender, EventArgs e)
{
HTMLDocument doc = webBrowser1.Document.DomDocument as HTMLDocument;
HtmlWindow window = webBrowser1.Document.Window.Frames[0].Frames[1];
doc = window.Document.DomDocument as HTMLDocument;
HTMLInputElementClass input = doc.all.item("gold", 0) as HTMLInputElementClass;
input.value = "1";
}
//阻止alert[警告框],confirm[确认对话框]对话框
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
//vDocument.parentWindow.execScript(
// "function alert(str){if(str=='未输入下注金额')confirm(str);}", "javaScript");
HtmlDocument doc = webBrowser1.Document.Window.Frames[0].Frames[1].Document;
//框架结构中,必须正确的指定目标框架
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = @"function confirm() {";
s += @"return true;";
s += @"}";
s += @"function alert() {}";
win.execScript(s, "javascript");
}
//***********************************************************************************************
//HtmlWindow window = webBrowser1.Document.Window.Frames[0].Frames[2].Frames[1];
//htmlDoc = window.Document.DomDocument as HtmlDocument;
////this.textBox2.Text = htmlDoc.Body.InnerHtml;
//按钮点击事件
//HtmlElement btnElement = htmlDoc.All["btnClose"];
//if (btnElement != null)
//{
// btnElement.Click += new HtmlElementEventHandler(btnElement_Click);
//}
//doc.GetElementById("SearchUnitBtn").InvokeMember("click");
//*******************************************************************************************
////存取网页的DOM元素:
////HtmlElement euser = webBrowser1.Document.All["txtUserName"];
////HtmlElement bSubmit = webBrowser1.Document.All["Button1"];
////euser.SetAttribute("value","想设置的值"); //设置DOM元素value属性
////bSubmit.InvokeMember("click"); //激发该元素的click事件
}
}