感覺很久不寫模擬器代碼了,昨天調(diào)試的時候碰了點壁,記錄下來,避免大家再跟我犯同樣的錯誤。
加入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)用的時候,只能出現(xiàn)函數(shù)名與參數(shù)值列表,不能增加其他內(nèi)容,否則調(diào)用就不會成功。
使用的腳本代碼:(這里的腳本代碼模擬了鼠標(biāo)移動的基礎(chǔ)需求,通過Js直接發(fā)鼠標(biāo)事件的方式來實現(xiàn)自動機器人)
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寫模擬器時的javascript腳本調(diào)用問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對html5模板網(wǎng)網(wǎng)站的支持!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!