【问题标题】:Trying to fill table element by id with casperjs试图用 casperjs 按 id 填充表格元素
【发布时间】:2021-09-05 06:14:05
【问题描述】:
casper.thenEvaluate(function(text){
    document.querySelector("#inputValue").value=text
})

casper.thenEvaluate 似乎不接受该参数,它在网页中填充 undefined。

【问题讨论】:

  • 我不是专家,但似乎应该在thenEvaluate 方法的调用中设置一个术语。类似casper.thenEvaluate(function(text){ document.querySelector("#inputValue").value=text }, 'term here')docs.casperjs.org/en/latest/modules/casper.html#thenevaluate
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: javascript web-scraping phantomjs screen-scraping casperjs


【解决方案1】:

根据文档,您应该在“开始”之后使用“thenEvaluate”,如下所示:

// Querying for "Chuck Norris" on Google
casper.start('http://google.fr/').thenEvaluate(function(term) {
    document.querySelector('input[name="q"]').setAttribute('value', term);
    document.querySelector('form[name="f"]').submit();
}, 'Chuck Norris');

casper.run();

来源:

http://docs.casperjs.org/en/latest/modules/casper.html#thenevaluate

【讨论】:

    【解决方案2】:

    由于评估在浏览器上下文中运行(而不是 Casper 之一),因此您必须在回调函数定义之后显式提供变量。请查看文档中的示例

    casper.evaluate(function(username, password) {
      document.querySelector('#username').value = username;
      document.querySelector('#password').value = password;
      document.querySelector('#submit').click();
    }, 'sheldon.cooper', 'b4z1ng4');
    

    注意,回调中的用户名参数对应sheldon.cooper,密码参数对应b4z1ng4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多