C#WebBrowser控件使用教程与技巧收集
来源:互联网 发布:送餐抢单软件哪个好 编辑:程序博客网 时间:2024/06/03 13:24
文章来自:http://www.sufeinet.com/thread-3941-1-1.html
先来看看常用的方法
Navigate(string urlString):浏览urlString表示的网址Navigate(System.Uri url):浏览url表示的网址Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息//(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去)GoBack():后退GoForward():前进Refresh():刷新Stop():停止GoHome():浏览主页WebBrowser控件的常用属性:Document:获取当前正在浏览的文档DocumentTitle:获取当前正在浏览的网页标题StatusText:获取当前状态栏的文本Url:获取当前正在浏览的网址的UriReadyState:获取浏览的状态WebBrowser控件的常用事件:DocumentTitleChanged,CanGoBackChanged,CanGoForwardChanged,DocumentTitleChanged,ProgressChanged,ProgressChangedDocumentCompleted 页面加载完成之后的事件
webBrowser1.Document.All["控件ID"].InnerText;或webBrowser1.Document.GetElementById("控件ID").InnerText;或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
webBrowser1.Document.All["控件ID"].GetAttribute("value");;或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
//输入框user.InnerText = "myname";password.InnerText = "123456";webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");
//下拉框:secret.SetAttribute("value", "question1"); //复选框rememberme.SetAttribute("Checked", "True");//多选框cookietime.SetAttribute("checked", "checked");
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
6、获取Div或其他元素的样式:
webBrowser1.Document.GetElementById("addDiv").Style;
Object[] objArray = new Object[1];objArray[0] = (Object)this.labFlightNumber.Text;webBrowser1.Document.InvokeScript("ticketbook", objArray);webBrowser1.Document.InvokeScript("return false");
HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;btnAdd.InvokeMember("Click");
this.timer1.Enabled = true;this.timer1.Interval = 1000 * 2;private void timer1_Tick(object sender, EventArgs e){ this.timer1.Enabled = false; ClickBtn.InvokeMember("Click");//执行按扭操作}
将WebBrowser控件ScriptErrorsSuppressed设置为True即可
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e){ //自动点击弹出确认或弹出提示 IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认 vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){ //自动点击弹出确认或弹出提示 IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认 vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示 //下面是你的执行操作代码}
13、获取网页中的Iframe,并设置Iframe的src
HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document; 或HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document; docFrame.All["mainFrame"].SetAttribute("src", "http://www.sufeinet.com/");
14、让控件聚焦
this.webBrowser1.Select();this.webBrowser1.Focus();doc.All["TPL_password_1"].Focus();
webBrowser1.Navigate(Application.StartupPath + @"\Test.html");
//根据Name获取元素public HtmlElement GetElement_Name(WebBrowser wb,string Name){ HtmlElement e = wb.Document.All[Name]; return e;} //根据Id获取元素public HtmlElement GetElement_Id(WebBrowser wb, string id){ HtmlElement e = wb.Document.GetElementById(id); return e;} //根据Index获取元素public HtmlElement GetElement_Index(WebBrowser wb,int index){ HtmlElement e = wb.Document.All[index]; return e;} //获取form表单名name,返回表单public HtmlElement GetElement_Form(WebBrowser wb,string form_name){ HtmlElement e = wb.Document.Forms[form_name]; return e;} //设置元素value属性的值public void Write_value(HtmlElement e,string value){ e.SetAttribute("value", value);} //执行元素的方法,如:click,submit(需Form表单名)等public void Btn_click(HtmlElement e,string s){ e.InvokeMember(s);}
[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); private static string GetCookieString(string url) { uint datasize = 1024; StringBuilder cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero)) { if (datasize < 0) return null; cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero)) return null; } return cookieData.ToString(); } private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e) { richTextBox1.Text = string.Empty; if (cbcookie.Checked) { if (checkBox1.Checked) { richTextBox1.Text = GetCookieString(textBox1.Text.Trim()); } else { richTextBox1.Text = webBrowser1.Document.Cookie; } } }
http://www.sufeinet.com/thread-2242-1-1.html
19.怎么在加载完成某个页面之后执行代码
//本事件是当每次加载完成当前页面后才会执行的 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { //e.Url是当前加载的页面, if (e.Url.ToString().Contains("http://sufeinet.com")) { //执行操作1 } else if (e.Url.ToString().Contains("http://baidu.com")) { //执行操作2 } }
private void webBrowser1_NewWindow(object sender, CancelEventArgs e){ string url = ((System.Windows.Forms.WebBrowser)sender).StatusText; webBrowser1.Navigate(url); e.Cancel = true; }
21.怎么设置Cookie
<span style="font-size:10px;color:#000000;">webBrowser1.Document.Cookie=“你的Cookie值”;</span>
0 0
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集 - 转
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
- WebBrowser控件使用技巧
- WebBrowser控件一些使用技巧
- WebBrowser控件使用技巧分享
- WebBrowser控件使用技巧分享
- Webbrowser控件使用的经验和技巧
- Winform中WebBrowser控件的使用技巧
- WebBrowser控件技巧
- WebBrowser控件技巧
- WebBrowser页面与WinForm交互技巧终极解决方案教程实例
- WebBrowser使用技巧
- 欢迎使用CSDN-markdown编辑器
- 条形码解析工具类
- 唾液的妙用
- PHP重定向的具体实现方式
- 代码块执行顺序
- C#WebBrowser控件使用教程与技巧收集
- HTTPSQS
- 电梯
- vm下NAT模式连接网络的设置
- 深入理解HTTP协议
- 迷恋&喜爱
- win8不显示缩略图
- Infobright 安装与配置
- OWASP Top 10 – 2013, 最新十大安全隐患(ASP.NET解决方法)