为什么不'这是Nightmarejs中修补的异步输入工作

Why doesn't this patched asynchronous input work in Nightmarejs?

本文关键字:修补 异步 输入 工作 Nightmarejs 这是 为什么不      更新时间:2023-09-26

我今晚在试验噩梦,我想在运行时有一个需要用户输入的步骤。有没有办法让噩梦允许这样的事情发生?我修改了lib附带的type()方法,并能够停止执行以输入一些文本。脚本甚至完成了,但我看不到我键入的输入,显示在它应该进入的文本框中。我确信dom选择器是正确的,因为当我使用相同的选择器和内置的type()方法时,静态文本就在那里。在下面的代码中,prompt(msg)是来自sync prompt的一个方法。这是我写的方法,但它不会更新页面上的元素:

exports.type_async = function(selector, msg, done) {
  this.page.evaluate(function(selector) {
    return document.querySelector(selector);
  }, function(element){
    text = prompt(msg);
    element.text = text;
    done();
  }, selector);
};

我做错了什么?

原始type在页面上下文中包含行element.value = text;(在evaluate内部,但不是结果回调),但您有element.text = text;

此外,该值可能应该在您提示输入之后设置

exports.typeAsync = function(selector, done) {
  var text = prompt('dynamic, blocking input:');
  debug('.type() %s into %s', text, selector);
  this.page.evaluate(function(selector, text) {
    var element = document.querySelector(selector);
    element.value = text;
  }, done, selector, text);
};

它被称为:

nightmare.typeAsync('#foo')