有没有办法使用 Nightmare (nodeJS模块)来按箭头键?是通过将按键发送到特定元素,还是通过单击该元素并将按键发送到整个页面?
我的问题是有一个下拉菜单,单击该菜单时,会在其下方显示一些选项。这些选项似乎不可点击,我尝试过 click 甚至 realClick Nightmare 扩展。似乎都无法选择下拉列表中的任何元素。在浏览该页面时,我发现通过使用箭头键和回车键,我可以选择选项而无需单击。那么有没有办法将按键事件发送到Electron呢?是通过 Nightmare 还是直接访问 Electron ?
最佳答案
这称为自动完成菜单,并且没有标准的方法。 我尝试与谷歌合作并想出了一个方法。我认为,制定通用解决方案将很困难,因为这取决于自动完成的实现方式。希望这有帮助!
var Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true,
webPreferences: {}
})
nightmare
.goto('http://www.google.co.in')
.evaluate(function(searchTerm) {
var elem = document.querySelector('#lst-ib');
elem.value = searchTerm;
//Do mousedown to initiate auto complete window
var event = document.createEvent('MouseEvent');
event.initEvent('mousedown', true, true);
elem.dispatchEvent(event);
}, 'Leo')
.then(function() {
//Wait for results to appear
nightmare.wait(1000)
.evaluate(function() {
//Click the first option of List
var first = document.querySelector('#sbtc > div.gstl_0.sbdd_a > div:nth-child(2) > div.sbdd_b > div > ul > li:nth-child(1)');
first.firstElementChild.click();
}).then(function() {
console.log('All Done');
});
});
关于node.js - 如何使用 Nightmare 按方向键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44725484/