zoukankan      html  css  js  c++  java
  • WPF的webBrowser控件关键代码

    1、根据元素ID获取元素的值。

    比如要获取<img class="" id="regimg" src="/register/checkregcode.html?1287068791" width="80" height="22">这个标签里的src属性的值:

    mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)webBrowser1.Document; mshtml.IHTMLElement img = (mshtml.IHTMLElement)doc2.all.item("regimg", 0);

    string imgUrl = (string)img.getAttribute("src");

    2、填写表单,并确定

    mshtml.IHTMLElement loginname = (mshtml.IHTMLElement)doc2.all.item("loginname", 0);     mshtml.IHTMLElement loginPW = (mshtml.IHTMLElement)doc2.all.item("password", 0);     mshtml.IHTMLElement loginBT = (mshtml.IHTMLElement)doc2.all.item("formsubmit", 0);     mshtml.IHTMLElement loginYZ = (mshtml.IHTMLElement)doc2.all.item("regcode", 0);     loginname.setAttribute("value", tbLoginName.Text);
    loginPW.setAttribute("value", tbLoginPassWord.Password); 
    loginYZ.setAttribute("value", tbYZ.Text);    
    loginBT.click(); 

    3、获取源码

    textBox1.Text = doc2.body.innerHTML;

    4、执行JS

    mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;
    win.execScript("changeRegImg()", "javascript");//使用JS

    5、禁止JS,WPF下目前发现唯一适用的一种方法:

    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 });

    }

       void webBrowser1_Navigated(object sender, NavigationEventArgs e)   
    {

        HideScriptErrors(webBrowser1,  true);

    }

  • 相关阅读:
    好的开源项目汇总
    强制SVN上传代码时添加日志
    微信开发-回调模式
    Struct2中自定义的Filter无效
    Ajax 传包含集合的JSON
    PostgreSQL数据库PL/PGSQL学习使用
    单用户对比PG 9.5.4和SYBASE 15.7对超大表的操作性能
    一场一波三折的SQL优化经历
    聚簇索引对数据插入的影响
    磁盘IO初探
  • 原文地址:https://www.cnblogs.com/sekon/p/4189747.html
Copyright © 2011-2022 走看看