感覺(jué)很久不寫(xiě)模擬器代碼了,昨天調(diào)試的時(shí)候碰了點(diǎn)壁,記錄下來(lái),避免大家再跟我犯同樣的錯(cuò)誤。
加入Javascript腳本的地方:
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)用的地方:
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ǔ)需求,通過(guò)Js直接發(fā)鼠標(biāo)事件的方式來(lái)實(shí)現(xiàn)自動(dòng)機(jī)器人)
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寫(xiě)模擬器時(shí)的javascript腳本調(diào)用問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)html5模板網(wǎng)網(wǎng)站的支持!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!