webbrowser頁面內容填充及光標定位

1、填充數據

a)SetAttribute("屬性","值")

WebBrowser.Document.GetElementById(“ID”).SetAttribute("屬性","值");

WebBrowser.Document.All[“Name”].SetAttribute("屬性","值");

WebBrowser.Document.GetElementsByTagName("元素")[index].SetAttribute("屬性","值");

b)InnerHtml、InnerText

WebBrowser.Document.GetElementById(“ID”).InnerHtml("值");

WebBrowser.Document.GetElementById(“ID”).InnerText("值");


2、觸發事件

Focus()

InvokeMember(“click”)//執行腳本函數click、focus之類


3、光標定位到頁面元素上

先引用一個Microsoft.mshtml

IHTMLWindow2 win = (IHTMLWindow2)WebBrowser.Document.Window.DomWindow;

string scriptLine = @"document.getElementById('login').focus()";

win.execScript(scriptLine, "Javascript");

{執行操作1}

通過JS來給光標定位,前提是要能找到該元素

這裏同樣可以用來定義其它的JS操作,改下JS語句就OK……

然後還有一個光標切換位置的:

scriptLine = "setTimeout(\"document.getElementById('pass').focus()\", 500)";
win.execScript(scriptLine, "Javascript");

我這裏是用settimeout,過一段時間然後換位置,沒找到其它更好,更簡單的方法,目前我也就只能這麼用了……

直接:

scriptLine = @"document.getElementById('loginPassword').focus()";
win.execScript(scriptLine, "Javascript");

{執行操作2}

這樣兩次操作都會在操作2裏,不知道是個什麼原因……

所以用了上面的settimeout


部分內容來自:http://blog.csdn.net/xjj800211/article/details/7814196




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章