关键词搜索

源码搜索 ×
×

C#WebBrowser控件使用教程与技巧收集

发布2015-09-09浏览1466次

详情内容

文章来自:http://www.sufeinet.com/thread-3941-1-1.html

先来看看常用的方法

  1. Navigate(string urlString):浏览urlString表示的网址
  2. Navigate(System.Uri url):浏览url表示的网址
  3. Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息
  4. //(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去)
  5. GoBack():后退
  6. GoForward():前进
  7. Refresh():刷新
  8. Stop():停止
  9. GoHome():浏览主页
  10. WebBrowser控件的常用属性:
  11. Document:获取当前正在浏览的文档
  12. DocumentTitle:获取当前正在浏览的网页标题
  13. StatusText:获取当前状态栏的文本
  14. Url:获取当前正在浏览的网址的Uri
  15. ReadyState:获取浏览的状态
  16. WebBrowser控件的常用事件:
  17. DocumentTitleChanged,
  18. CanGoBackChanged,
  19. CanGoForwardChanged,
  20. DocumentTitleChanged,
  21. ProgressChanged,
  22. ProgressChanged
  23. DocumentCompleted 页面加载完成之后的事件

1、获取非input控件的值:
  1. webBrowser1.Document.All["控件ID"].InnerText;
  2. 或webBrowser1.Document.GetElementById("控件ID").InnerText;
  3. 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
2.获取input控件的值:
  1. webBrowser1.Document.All["控件ID"].GetAttribute("value");;
  2. 或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
3、给输入框赋值:
  1. //输入框
  2. user.InnerText = "myname";
  3. password.InnerText = "123456";
  4. webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");
4、下拉、复选、多选:
  1. //下拉框:
  2. secret.SetAttribute("value", "question1");
  3. //复选框
  4. rememberme.SetAttribute("Checked", "True");
  5. //多选框
  6. cookietime.SetAttribute("checked", "checked");
5、根据已知有ID的元素操作没有ID的元素:
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。
6、获取Div或其他元素的样式:
webBrowser1.Document.GetElementById("addDiv").Style;
7、直接执行页面中的脚本函数,带动态参数或不带参数都行
  1. Object[] objArray = new Object[1];
  2. objArray[0] = (Object)this.labFlightNumber.Text;
  3. webBrowser1.Document.InvokeScript("ticketbook", objArray);
  4. webBrowser1.Document.InvokeScript("return false");
8、自动点击、自动提交:
  1. HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
  2. btnAdd.InvokeMember("Click");

9、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:
  1. this.timer1.Enabled = true;
  2. this.timer1.Interval = 1000 * 2;
  3. private void timer1_Tick(object sender, EventArgs e)
  4. {
  5. this.timer1.Enabled = false;
  6. ClickBtn.InvokeMember("Click");//执行按扭操作
  7. }
10、屏蔽脚本错误:
将WebBrowser控件ScriptErrorsSuppressed设置为True即可
11、自动点击弹出提示框:
  1. private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  2. {
  3. //自动点击弹出确认或弹出提示
  4. IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
  5. vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
  6. vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
  7. }
12.WebBrowser页面加载完毕之后,在页面中进行一些自动化操作的时候弹出框的自动点击(屏蔽)

  1. private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  2. {
  3. //自动点击弹出确认或弹出提示
  4. IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
  5. vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
  6. vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
  7. //下面是你的执行操作代码
  8. }

13、获取网页中的Iframe,并设置Iframe的src

  1. HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document;
  2. HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document;
  3. docFrame.All["mainFrame"].SetAttribute("src", "http://www.sufeinet.com/");
网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
14、让控件聚焦
  1. this.webBrowser1.Select();
  2. this.webBrowser1.Focus();
  3. doc.All["TPL_password_1"].Focus();
15、打开本地网页文件
webBrowser1.Navigate(Application.StartupPath + @"\Test.html");
16、获取元素、表单
  1. //根据Name获取元素
  2. public HtmlElement GetElement_Name(WebBrowser wb,string Name)
  3. {
  4. HtmlElement e = wb.Document.All[Name];
  5. return e;
  6. }
  7. //根据Id获取元素
  8. public HtmlElement GetElement_Id(WebBrowser wb, string id)
  9. {
  10. HtmlElement e = wb.Document.GetElementById(id);
  11. return e;
  12. }
  13. //根据Index获取元素
  14. public HtmlElement GetElement_Index(WebBrowser wb,int index)
  15. {
  16. HtmlElement e = wb.Document.All[index];
  17. return e;
  18. }
  19. //获取form表单名name,返回表单
  20. public HtmlElement GetElement_Form(WebBrowser wb,string form_name)
  21. {
  22. HtmlElement e = wb.Document.Forms[form_name];
  23. return e;
  24. }
  25. //设置元素value属性的值
  26. public void Write_value(HtmlElement e,string value)
  27. {
  28. e.SetAttribute("value", value);
  29. }
  30. //执行元素的方法,如:click,submit(需Form表单名)等
  31. public void Btn_click(HtmlElement e,string s)
  32. {
  33. e.InvokeMember(s);
  34. }
17.获取Cookie
  1. [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
  2. static extern bool InternetGetCookieEx(string pchUrl, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
  3. private static string GetCookieString(string url)
  4. {
  5. uint datasize = 1024;
  6. StringBuilder cookieData = new StringBuilder((int)datasize);
  7. if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))
  8. {
  9. if (datasize < 0)
  10. return null;
  11. cookieData = new StringBuilder((int)datasize);
  12. if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
  13. return null;
  14. }
  15. return cookieData.ToString();
  16. }
  17. private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
  18. {
  19. richTextBox1.Text = string.Empty;
  20. if (cbcookie.Checked)
  21. {
  22. if (checkBox1.Checked)
  23. {
  24. richTextBox1.Text = GetCookieString(textBox1.Text.Trim());
  25. }
  26. else
  27. {
  28. richTextBox1.Text = webBrowser1.Document.Cookie;
  29. }
  30. }
  31. }
18.怎么设置代理
http://www.sufeinet.com/thread-2242-1-1.html
19.怎么在加载完成某个页面之后执行代码
  1. //本事件是当每次加载完成当前页面后才会执行的
  2. private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  3. {
  4. //e.Url是当前加载的页面,
  5. if (e.Url.ToString().Contains("http://sufeinet.com"))
  6. {
  7. //执行操作1
  8. }
  9. else if (e.Url.ToString().Contains("http://baidu.com"))
  10. {
  11. //执行操作2
  12. }
  13. }
20.怎么禁止在新窗口中打开网页
  1. private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
  2. {
  3. string url = ((System.Windows.Forms.WebBrowser)sender).StatusText;
  4. webBrowser1.Navigate(url);
  5. e.Cancel = true;
  6. }

21.怎么设置Cookie

<span style="font-size:10px;color:#000000;">webBrowser1.Document.Cookie=“你的Cookie值”;</span>



相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载