我需要模拟类似按F5触发刷新网站的内容。 这个想法是使用sendKeysAndWait $ {KEY_12},但据我所知,我需要一个元素作为该命令的目标。
使用Webdriver我解决了这样的问题:
Actions action = new Actions(_driver);
action.sendKeys(Keys.F12).perform();
有没有办法将F12或任何其他密钥发送到页面?
我尝试将事件发送到正文,但不起作用。
我尝试将事件发送到我发现的第一个输入(这可行,但我不想这样做,有些网站没有任何输入元素在appplication中)
我最后的希望是一个javascript文件。 我创建了一个函数并将其添加到Selenium Core扩展中:
Selenium.prototype.doHotkey = function(target,value) {
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod](
"keypress", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
value, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
// window.dispatchEvent(keyboardEvent); // does nothing
// document.dispatchEvent(keyboardEvent); // does nothing
body.dispatchEvent(keyboardEvent); // is undefined
}
但在这里我发现,那个窗口根本不是我的页面。有什么提示吗?
答案 0 :(得分:0)
我必须使用JS文件和Selenium Core扩展(将代码保存在JS文件中并将其添加到options / general下的扩展名),然后您可以使用
调用它command : hotkey
target :
Value : 13 // for enter
JS:
Selenium.prototype.doHotkey = function(target,value) {
var win = this.browserbot.getCurrentWindow(); // this way you get your site
var doc = win.document;
var keyboardEvent = doc.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
win, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
value, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
doc.body.dispatchEvent(keyboardEvent);
}