WPF使用Webbrowser操作网页的主要代码

摘要:
stringgimgUrl=(string)img.getAttribute(“src”);tbYZ.Text);strings=@“window.alert=null;window.confirm=null;window.showModalDialg=null;
1,引用mshtml.dll

using mshtml;
2,获取元素属性值

IHTMLDocument2 doc2=(IHTMLDocument)webbrowser1.Document;
IHTMLElement img=(IHTMLElement)doc2.all.item("regimg",0);
string imgUrl=(string)img.getAttribute("src");
3,取表单控件


IHTMLElement loginName=(IHTMLElement)doc2.all.item("loginname",0);
IHTMLElement loginPW=(IHTMLElement)doc2.all.item("password",0);
IHTMLElement loingYZM=(IHTMLElement)doc2.all.item("regcode",0);
IHTMLElement loginBT=(IHTMLElement)doc2.all.item("formsubmit",0);
4,填写表单控件

loginName.setAttribute("value",tbLoginName.Text);
loginPW.setAttribute("value",tbLoginPassWord.Password);
loginYZM.setAttribute("value",tbYZ.Text);
5,点击按钮

loginBT.click();
6,执行js脚本

方法1:

IHTMLwindow win=(IHTMLWindow2)doc2.parentWindows;
win.execScript("alert('hello!')","javascript");
方法2:

webbrowser1.InvokeScript("eval","alert('hello!')");
7,屏蔽alert、confirm等,通过重定义实现

private voie webbrowser1_navigated(object sender,WebBroserNavigatedEventArgs e)
{
    IHTMLWindow2 win=(IHTMLWindow2)webbrowser1.Document.Window.DomWindow;
    string s=@"window.alert=null; window.onerror=null;window.confirm=null; windows.open=null; window.showModalDialg=null;";
    win.execScript(s,"javascript");
}
8,接收js消息

复制代码
[ComVisible(true)]  //这句要加到类定义前,可与COM通信

private void webbrowser1_Navigated(objec sender,WebBrowserNavigatedEventargs e)
{
    IHTMLWindow2 win=(IHTMLWindow2)webbrowser1.Document.Window.DomWindow;
    //假设把alert消息传出来处理
    string s=@"function alert(str){window.external.procMessage(str);}";
    win.execScript(s,"javascript");
    webbrowser1.ObjectForScripting=this;  //指定脚本消息送到当前实例处理
}

//处理脚本消息的方法
public void procMessage(string s)
{
    MessageBox.Show("脚本消息:"+s);
}

免责声明:文章转载自《WPF使用Webbrowser操作网页的主要代码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇R语言-自动批处理idea在处理spring国际化解决中文乱码,properties的格式:native-to-ascii下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android跨应用启动Service

Android5.0之后规定只能通过显示Intent启动服务,所以掌握以下的启动方式很有必要 步骤一:创建两个安卓项目one,two 步骤二:在项目一中创建一个自定义类继承Service MyIntentService.java package com.contentprovide.liuliu.a2_3; import android.app.Int...

SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active

趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 java -jar app.jar --spring.profiles.active=qa --server.port=8060 //生产环境 java -jar...

MySQL索引优化

一、单表 创建索引之前:type=ALL全表扫描,Extra里面的Using filesort(文件内部排序) 根据where后面的条件创建:CREATE INDEX idx_article_ccv ON article(category_id,comments,views);    可以看出type由ALL变成了range,但是Extra里面的Usi...

ActiveMQ教程(简介与安装)

  ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。   一、ActiveMq的特性:   ⒈ 多种语言和协议编写客户端。语言: Java,...

1. Linux驱动开发之开篇--Makefile

基本Makefile假设现在有3个文件,file2.h是函数声明,file2.c是函数定义,文件file1.c调用file2.c中的函数。则Makefile文件的编写如下: helloworld:file1.o file2.o gcc file1.o file2.o -o helloworld file1.o:file1.c file2.h...

vmware 下找不到ifcfg-eth0的问题

找不大 eth0网卡,也就连不上网络,症状是ifconfig以后只现实lo,不显示eth0 ifconfig,显示的ip是ifcfg-lo的ip 解决办法 1. 拷贝cp ifcfg-lo ifcfg-eth0 2. 重新配置ifcfg-eth0如下 DEVICE="eth0" BOOTPROTO="static" NM_CONTROLLED="ye...