感覺很久不寫模擬器代碼了,昨天調(diào)試的時(shí)候碰了點(diǎn)壁,記錄下來,避免大家再跟我犯同樣的錯(cuò)誤。
加入Javascript腳本的地方:
1
2
3
4
|
HtmlElement jsElement = webBrowser1.Document.CreateElement( "script" ); jsElement.SetAttribute( "type" , "text/javascript" ); jsElement.SetAttribute( "text" , "showMeAction = function(e) { window.alert(e);}" ); webBrowser1.Document.Body.AppendChild(jsElement); |
調(diào)用的地方:
1
2
3
|
string[] args = new string[1]; args[0] = "Hello element!" ; webBrowser1.Document.InvokeScript( "showMeAction" , args); |
大家特別注意的是后面腳本調(diào)用的時(shí)候,只能出現(xiàn)函數(shù)名與參數(shù)值列表,不能增加其他內(nèi)容,否則調(diào)用就不會(huì)成功。
使用的腳本代碼:(這里的腳本代碼模擬了鼠標(biāo)移動(dòng)的基礎(chǔ)需求,通過Js直接發(fā)鼠標(biāo)事件的方式來實(shí)現(xiàn)自動(dòng)機(jī)器人)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function createEvent(eventName, ofsx, ofsy) { var evt = document.createEvent( 'MouseEvents' ); evt.initMouseEvent(eventName, true , false , null , 0, 0, 0, ofsx, ofsy, false , false , false , false , 0, null ); return evt; } function moveElement(pxToMove) { var sliderKnob = document.getElementsByClassName( "gt_slider_knob" )[0]; var boxRect = sliderKnob.getBoundingClientRect(); var move = createEvent( 'mousemove' , boxRect.left + sliderKnob.offsetLeft + pxToMove, boxRect.top + sliderKnob.offsetTop); var down = createEvent( 'mousedown' , boxRect.left + sliderKnob.offsetLeft, boxRect.top + sliderKnob.offsetTop); var up = createEvent( 'mouseup' ); sliderKnob.dispatchEvent(down); document.dispatchEvent(move); sliderKnob.dispatchEvent(up); } |
以上所述是小編給大家介紹的使用C# 的webBrowser寫模擬器時(shí)的javascript腳本調(diào)用問題,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/jackxinxu2100/article/details/74905348?utm_source=tuicool&utm_medium=referral