四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2112|回复: 0
打印 上一主题 下一主题

C#中的WebBrowser控件的使用

[复制链接]
  • TA的每日心情
    开心
    2022-6-10 09:59
  • 366

    主题

    741

    帖子

    9649

    积分

    超级版主

    Rank: 8Rank: 8

    积分
    9649
    跳转到指定楼层
    楼主
    发表于 2016-11-4 10:07:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    作者:txw1958
    原文:http://www.cnblogs.com/txw1958/a ... arp-WebBrowser.html

    0、常用方法

    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
    复制代码



    1、获取非input控件的值:
    webBrowser1.Document.All["控件ID"].InnerText;
    或webBrowser1.Document.GetElementById("控件ID").InnerText;
    或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");

    2、获取input控件的值:
    webBrowser1.Document.All["控件ID"].GetAttribute("value");;
    或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");


    3、给输入框赋值:
    //输入框
    user.InnerText = "myname";
    password.InnerText = "123456";
    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、直接执行页面中的脚本函数,带动态参数或不带参数都行:
    Object[] objArray = new Object[1];
    objArray[0] = (Object)this.labFlightNumber.Text;
    webBrowser1.Document.InvokeScript("ticketbook", objArray);
    webBrowser1.Document.InvokeScript("return false");


    8、自动点击、自动提交:
    HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
    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. }
    复制代码

    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. }
    复制代码



    12、获取网页中的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.baidu.com/");


    13、网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。


    14、让控件聚焦
    this.webBrowser1.Select();
    this.webBrowser1.Focus();
    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. }, word.Text);
    复制代码


    看看 使用很简单吧。给大家个实例http://files.cnblogs.com/zhuxiangyu/Tessnet2example.rar用来测试。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-7 02:23 , Processed in 1.078125 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表