一、相关操作设置
//1.初始化配置
private void Init() {
//1.屏蔽脚本错误弹窗提示
webBrowser1.ScriptErrorsSuppressed = true;
//2.打开百度页面
webBrowser1.Navigate("http://www.baidu.com");
//3.禁止快捷键
webBrowser1.WebBrowserShortcutsEnabled = false;
//4.禁止右键菜单
webBrowser1.IsWebBrowserContextMenuEnabled = false;
//禁止拖放
webBrowser1.AllowWebBrowserDrop = false;
}
//2.页面加载完成事件
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
//加载完成事件应判断事件地址与浏览器是否一致,因为此方法会多次执行
if (e.Url.ToString() != webBrowser1.Url.ToString()) {
return;
}
if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) {
return;
}
//将地址栏地址设置为当前页面地址
url_textBox1.Text = webBrowser1.Url.ToString();
//设置id为"kw"的组件值为"你好"
webBrowser1.Document.GetElementById("kw").SetAttribute("value", "你好");
//设置name='tel' 的组件值为
HtmlElement tel_whoNode = webBrowser1.Document.All["tel"];
tel_whoNode.SetAttribute("value", "123456");
//触发name为"cityid"的组件的onchange事件
HtmlElement cityidNode = webBrowser1.Document.All["cityid"];
cityidNode.SetAttribute("value", cityid);
cityidNode.InvokeMember("onchange");
//获取网页源代码(指定字符编码,防止乱码)
StreamReader sr = new StreamReader(webBrowser1.DocumentStream,
Encoding.GetEncoding(("gbk")));
string strhtml = sr.ReadToEnd();
}
//新标签弹窗事件
private void _browser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e) {
// 禁止弹出新窗体
e.Cancel = true;
}
关于webBrowser好像不能直接通过Class来获取标签,间接的方式就是先通过标签名获取所有组件,然后遍历判断组件的class值是否为你要的.
二、取存Cookie
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr dwReserved);
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
public static string GetCookieString(string url) {
// Determine the size of the cookie
uint datasize = 2560;
StringBuilder cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero)) {
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
return null;
}
return cookieData.ToString();
}
//取Cookie示例-----------------------------------
string cookie = CookieService.GetCookieString(webBrowser1.Url.ToString());
//存Cookie示例:按字段分别存,url指你要设置的Cookie的域名-----------------------------------
CookieService.InternetSetCookie(url, "s_uid", "18600000004");
CookieService.InternetSetCookie(url, "s_pwd", "123456");
三、清理临时文件、历史记录、缓存等数据
/// <summary>
/// ShellExecute方法
///
/// 调用示例:清除IE临时文件
/// ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);
///
/// 其中ClearMyTracksByProcess 可进行选择设置 :
///
/// Temporary Internet Files (Internet临时文件)
/// RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 8
///
/// Cookies
/// RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 2
///
/// History (历史记录)
/// RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 1
///
/// Form.Data (表单数据)
/// RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 16
///
/// Passwords (密码)
/// RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 32
///
/// Delete All (全部删除)
/// RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 255
///
/// 解决方案2:快速清除webBrowser Cookie
/// wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count - 1))
///
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lpOperation"></param>
/// <param name="lpFile"></param>
/// <param name="lpParameters"></param>
/// <param name="lpDirectory"></param>
/// <param name="nShowCmd"></param>
/// <returns></returns>
[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
四、关于WebBrowser页面加载过程中,界面卡死/假死问题求助!
问题描述:关于WebBrowser这个控件的载体是一个new Form(),不是启动窗口,在加载页面或者需要延时循环判断过程中,窗口会出现假死状态,网上有说用Application.DoEvents();可以解决,但是我试了仍然不行,Thread也一样,不知道是我没设置好还是什么问题,恳请大神指教!事成之后必有私包重谢!
最后说一下使用WebBrowser感受,关于网页填表的话,能不用WebBrowser就不用WebBrowser,Selenium好好的干嘛要用WebBrowser?本人的初衷也是为了能使软件小巧,尽量不参杂其他插件配件的调用,但是还事与愿违,界面假死问题始终未解决,还有就是WebBrowser使用的是IE内核,很多页面无法正常显示.所以,能用selenium就用selenium吧!