mshtml与System.Windows.Forms.HtmlElement
2011-08-16 11:01
405 查看
(1)打开并操作外部IE,这个只有MSHTML能做到:
打开ie:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
object objFlags = 1;
object objTargetFrameName = "";
object objPostData = "";
object objHeaders = "";
SHDocVw.InternetExplorer webBrowser1= (SHDocVw.InternetExplorer)shellWindows.Item(shellWindows.Count-1);
webBrowser1.Navigate(“http://www.google.cn”, ref objFlags, ref objTargetFrameName, ref objPostData, ref objHeaders);
(2)如果里面有iframe,要给里面的text填充信息
HtmlDocument main_formDoc = webBrowser1.Document;
IHTMLDocument2 main_mshtmlIHTMLDoc = (IHTMLDocument2)main_formDoc.DomDocument;
Object frame_index = 0;
IHTMLWindow2 target_mshtmlIHTMLWindow = (IHTMLWindow2)main_mshtmlIHTMLDoc.frames.item(ref frame_index);
IHTMLDocument2 target_mshtmlIHTMLDoc = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(target_mshtmlIHTMLWindow);
HTMLDocument doc = target_mshtmlIHTMLDoc as HTMLDocument;
IHTMLElementCollection eCollection = doc.getElementsByTagName("input");
foreach (IHTMLElement element in eCollection)
{
if(element.getAttribute("name")=="username")
element.innerHTML = name.Trim();
}
---------------------------------------------------------------------
HtmlElementCollection submits = webBrowser1.Document.GetElementsByTagName("select");
foreach (HtmlElement submit in submits)
{
if (submit.GetAttribute("name") == "typeid")
{
submit.SetAttribute("value", siteid);
//submit.InvokeMember("click");
break;
}
}
(3)要点击里面的某个按钮
mshtml.IHTMLDocument2 doc2=(mshtml.IHTMLDocument2)myWeb.Document;
mshtml.IHTMLElementCollection inputs;
inputs=(mshtml.IHTMLElementCollection)doc2.all.tags("INPUT");
mshtml.IHTMLElement element=(mshtml.IHTMLElement)inputs.item("SubmitBut",0);
element.click();
------------------------------------------------------
HtmlElement loginsubmit = webBrowser1.Document.GetElementById("loginsubmit");
loginsubmit.InvokeMember("click");
(4)根据元素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");
----------------------------------------------------------------
HtmlElement regbimp1 = webBrowser1.Document.GetElementById("regimg");
string imgUrl = (string)img.getAttribute("src");
(5)执行JS
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;
win.execScript("changeRegImg()", "javascript");//使用JS
-------------------------
IHTMLWindow2 login = (mshtml.IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
login.execScript("updateseccode()", "javascript");
(6)、禁止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);
}
打开ie:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
object objFlags = 1;
object objTargetFrameName = "";
object objPostData = "";
object objHeaders = "";
SHDocVw.InternetExplorer webBrowser1= (SHDocVw.InternetExplorer)shellWindows.Item(shellWindows.Count-1);
webBrowser1.Navigate(“http://www.google.cn”, ref objFlags, ref objTargetFrameName, ref objPostData, ref objHeaders);
(2)如果里面有iframe,要给里面的text填充信息
HtmlDocument main_formDoc = webBrowser1.Document;
IHTMLDocument2 main_mshtmlIHTMLDoc = (IHTMLDocument2)main_formDoc.DomDocument;
Object frame_index = 0;
IHTMLWindow2 target_mshtmlIHTMLWindow = (IHTMLWindow2)main_mshtmlIHTMLDoc.frames.item(ref frame_index);
IHTMLDocument2 target_mshtmlIHTMLDoc = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(target_mshtmlIHTMLWindow);
HTMLDocument doc = target_mshtmlIHTMLDoc as HTMLDocument;
IHTMLElementCollection eCollection = doc.getElementsByTagName("input");
foreach (IHTMLElement element in eCollection)
{
if(element.getAttribute("name")=="username")
element.innerHTML = name.Trim();
}
---------------------------------------------------------------------
HtmlElementCollection submits = webBrowser1.Document.GetElementsByTagName("select");
foreach (HtmlElement submit in submits)
{
if (submit.GetAttribute("name") == "typeid")
{
submit.SetAttribute("value", siteid);
//submit.InvokeMember("click");
break;
}
}
(3)要点击里面的某个按钮
mshtml.IHTMLDocument2 doc2=(mshtml.IHTMLDocument2)myWeb.Document;
mshtml.IHTMLElementCollection inputs;
inputs=(mshtml.IHTMLElementCollection)doc2.all.tags("INPUT");
mshtml.IHTMLElement element=(mshtml.IHTMLElement)inputs.item("SubmitBut",0);
element.click();
------------------------------------------------------
HtmlElement loginsubmit = webBrowser1.Document.GetElementById("loginsubmit");
loginsubmit.InvokeMember("click");
(4)根据元素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");
----------------------------------------------------------------
HtmlElement regbimp1 = webBrowser1.Document.GetElementById("regimg");
string imgUrl = (string)img.getAttribute("src");
(5)执行JS
mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow;
win.execScript("changeRegImg()", "javascript");//使用JS
-------------------------
IHTMLWindow2 login = (mshtml.IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
login.execScript("updateseccode()", "javascript");
(6)、禁止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);
}
相关文章推荐
- 无法将类型“System.Windows.Forms.HtmlDocument”转换为“mshtml.IHTMLDocument2” 的解决方法
- 无法将类型为“System.__ComObject”的 COM 对象强制转换为类类型“mshtml.HTMLInputElementClass
- System.Windows.Forms.DataGridView的使用总结
- System.Windows.Forms.PropertyGrid的使用
- 引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常解决方案
- vs2010 无法嵌入互操作类型“mshtml.HTMLAnchorElementClass”。请改用适用的接口
- System.Windows.Forms.Timer、System.Timers.Timer和System.Threading.Timer区别
- 警告 CS0105: “System.Windows.Forms"的using指令以前在此命名空间中出现过
- 引发“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常解决
- System.Windows.Forms.Timer定时器的“自毁”
- 根据输入内容,更改ListBox(System.Windows.Forms.ListBox)项显示内容
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用
- System.Windows.Forms中的Message Structure
- Csharp:TinyMCE HTML Editor in .NET WindowsForms
- Using System.Windows.Forms.Label and System.Windows.Forms.FontDialog
- 引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常 解决办法
- System.Windows.Forms.Application.ExecutablePath 路径问题
- System.Windows.Forms.Timer与System.Timers.Timer的区别 .
- 使用System.Windows.Forms.Application.DoEvents实时更新画面
- System.Windows.Forms.Timer和System.Timers.Timer的区别 [转]