一、调用JS函数并返回值
public void Pc(WebBrowser webBrowser)
{
//HTMLDocument html = webBrowser.Document as HTMLDocument;
string strRetVal = (string)webBrowser.InvokeScript("getXfjpcData");
}
二、利用Microsoft.mshtml
引用命令空间 using mshtml;
HTMLDocument doc = webbrowser.Document as HTMLDocument;
string html = doc.body.innerHTML;//取源码
Console.WriteLine(doc.cookie);//取cookies
//根据ID获取元素
//根据id获得input
IHTMLElement inputSearch = (IHTMLElement)doc.all.item("kw");
//为input设置value属性
inputSearch.setAttribute("value","设置搜索内容");
//获得搜索按钮
IHTMLElement submitSearch = (IHTMLElement)doc.all.item("su");
//点击按钮
submitSearch.click();
//获取窗体
mshtml.IHTMLWindow2 window = (mshtml.IHTMLWindow2)doc.parentWindow;
//注入javascript
window.execScript("alert(123);", "javascript");
//注入禁止弹窗等
window.execScript("function alert(){return;}", "javascript");
window.execScript("function confirm(){return;}", "javascript");
//第二次的alert将不会执行
window.execScript("alert(456);", "javascript");