使用 Selenium IDE 将 KEY 发送到页面

Sending a KEY to a Page with Selenium IDE

我需要模拟类似按 F5 触发站点刷新的操作。 这个想法是使用 sendKeysAndWait ${KEY_12},但据我了解,我需要一个元素作为该命令的目标。

使用 Webdriver 我解决了这样的问题:

Actions action = new Actions(_driver);
action.sendKeys(Keys.F12).perform();

有没有办法将 F12 或任何其他键发送到页面?

我尝试将事件发送到正文,但没有用。

我尝试将事件发送到我找到的第一个输入(这有效,但我不想这样做,有些网站在应用程序中没有任何输入元素)

我最后的希望是 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
}

但在这里我发现,window 根本不是我的主页。有什么建议吗?

我必须用一个 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);

}