一、調用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");