WebBrowser控件技巧

1.给页面注入Javascript并执行

'脚本内容为:javascript:document.Survey_Form.Action.value = 'SurveyOverSubmit';Survey_Form_Submit();

 Dim ele As HtmlElement
 ele = Web.Document.CreateElement("script")
 ele.SetAttribute("type", "text/javascript")
 ele.SetAttribute("text", "javascript:document.Survey_Form.Action.value = 'SurveyOverSubmit';Survey_Form_Submit();")
 Web.Document.Body.AppendChild(ele)

2.运行页面中javascript函数

webBrowser1.Document.InvokeScript(“someInPageJavaFunction”,
null)

3.关闭本错误弹出对话框

webbrowser.ScriptErrorsSuppressed=true

4.触发button按钮click事件

HtmlElement btn = webBrowser1.document.getElementByIdx_x(“aBtn”);

if(btn !=null)

   btn.InvokeMember(“click”);

5.读取,设置javacript变量

需要引用mshtml

        Dim vDocument As IHTMLDocument2 = TryCast(Web.Document.DomDocument, IHTMLDocument2)
        Dim vWindow As IHTMLWindow2 = DirectCast(vDocument.parentWindow, IHTMLWindow2)
        Dim vWindowType As Type = vWindow.[GetType]()
        Dim testText As Object = vWindowType.InvokeMember("tmp", BindingFlags.GetProperty, Nothing, vWindow, New Object() {})
        ' 读取
        'vWindowType.InvokeMember("tmp", BindingFlags.SetProperty, Nothing, vWindow, New Object() {"30000"})
        ' 设置
        'vWindowType.InvokeMember("setTimeout", BindingFlags.InvokeMethod, Nothing, vWindow, New Object() {"Timer()", 30000})
        ' 执行方法

6.获取HTML元素属性值

MessageBox.Show(Web.Document.GetElementById("verifyCode").GetAttribute("value"))

7.滚动到页面底部

Web.Document.Window.ScrollTo(0, Web.Document.Body.ScrollRectangle.Height)

8.HTML文本框获得焦点

Web.Document.GetElementById("verifyCode").Focus()

9.单选框同名时选择

Web.Document.All.GetElementsByName("option_7_" & x.ToString).Item(r.Next(2, 4) - 1)

10.触发SUBMIT按钮

Web.Document.Forms("Survey_Form").All("SurveyOverSubmit").InvokeMember("click")

11.设置元素属性

Web.Document.GetElementById("verifyCode").SetAttribute("value", "fdasfda")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值