node.js - 如何使用 Nightmare 按方向键?

标签 node.js electron nightmare

有没有办法使用 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/

相关文章:

node.js - Jade 和空白

node.js - 装饰器必须在属性声明的名称和所有关键字之前

javascript - 在 nightmare.js 中循环 url 时的异步挑战

javascript - 在服务器上通过pm2或类似的东西运行 Nightmare

json - NightmareJS 将变量保存到文件

node.js - brcypt 模块未对密码进行哈希处理

node.js - 如何使用 ffmpeg 将 Node 应用程序部署到 heroku?

javascript - 为什么在主进程和渲染进程中调用的 window.maximize() 与 macos 上的动画不同?

javascript - 如何使用 Electron 重新加载忽略多个目录和文件

javascript - Promise 返回 'undefined' 或 babel 编译的代码不等待返回(async/await)