WebBrowser控件[Windows窗体]之应用篇

来源:互联网 发布:淘宝上的组装电脑 编辑:程序博客网 时间:2024/04/30 10:51

WebBrowser控件[Windows窗体]之介绍篇中介绍WebBrowser控件功能以及写了一个小例子。这次主要介绍一下具体的应用: 在我写程序的过程中主要是在DocumentCompleted 事件(网页完成加载时发生)里操作。还是举例说明(主要实现的是自动寻找链接(包含“next”字符串的链接地址),然后跳转页面):

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {

WebBrowser wb = new WebBrowser();

wb = (WebBrowser)sender;

if (e.Url.ToString().Trim() == wb.Url.ToString())

{ this.NextPage();

}

}

private void NextPage(WebBrowser w)

{

HtmlDocument objDoc = w.Document;

if (w.Document.GetElementsByTagName("A") != null)

{

foreach (HtmlElement h in w.Document.GetElementsByTagName("A"))

{

if (h.InnerText != null && h.InnerText.ToString().IndexOf("next") != -1) // IndexOf("next")中的“next”是在next之间的字符串。

 {

obj.InvokeMember("Click"); // 执行Click事件,使页面跳转到下一页面。、

break;

}

}

}

}

原创粉丝点击