using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CefSharp.WinForms;
using CefSharp;
using System.Web;
using HtmlAgilityPack;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace BSSimple
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void buttonKanKan_Click(object sender, EventArgs e)
{
HtmlElement searchWords = webBrowser1.Document.All["kw"];//获取百度搜索的文本框 <br>
HtmlElement searchButton = webBrowser1.Document.All["su"];//获取百度搜索的按钮 <br>
searchWords.SetAttribute("value", this.Key.Text.Trim());//给百度搜索的文本框赋值 <br>
searchButton.InvokeMember("click");//调用百度搜索按钮的点击事件
}
public void HideScriptErrors(WebBrowser wb, bool Hide)
{
FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fiComWebBrowser == null)
return;
object objComWebBrowser = fiComWebBrowser.GetValue(wb);
if (objComWebBrowser == null)
return;
objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
}
private void FormMain_Load(object sender, EventArgs e)
{
this.AcceptButton = buttonKanKan;
this.webBrowser1.ScriptErrorsSuppressed = true;
this.webBrowser1.Navigate("http://www.baidu.com");
}
private void webBrowser1_Navigated()
{
HideScriptErrors(webBrowser1, true);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!e.Url.Equals(webBrowser1.Url))
{
//not actually complete, do nothing
return;
}
HideScriptErrors(webBrowser1, true);
if (this.webBrowser1.Url.ToString() != "https://www.baidu.com/")
{
HtmlElement target = webBrowser1.Document.GetElementById("content_left");
Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
MatchCollection mc = reg.Matches(target.InnerHtml);
string url = string.Empty;
foreach (Match m in mc)
{
url = m.Groups["url"].Value;
if (url.StartsWith("http"))
{
break;
}
}
System.Diagnostics.Process.Start(url);
}
}
}
}