javascript - Nightmarejs 同一测试中的多个页面

标签 javascript node.js nightmare

我正在尝试从 Drupal 站点上的两个网页中提取标题标签文本。我想使用 Nightmarejs。

这是迄今为止我的代码:

// get the <title> text from inside the Drupal site
var Nightmare = require('nightmare');
var user = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a5acaca1a2b183a6bba2aeb3afa6eda0acae" rel="noreferrer noopener nofollow">[email protected]</a>';
var pass = 'foobar';

new Nightmare()
  .goto('http://example.com/user')
    .type('#edit-name', user)
    .type('#edit-pass', pass)
    .click('.searchsubmit')
    .wait()
    .evaluate(function () {
       return document.getElementsByTagName("TITLE")[0];
       }, function (res) {
      console.log('Homepage title: '+res.text);
    })
    .run(function(err, nightmare){
      console.log('Done1.');

      // step 2
      nightmare
        .goto('http://example.com/admin')
        .wait()
        .evaluate(function () {
          return document.getElementsByTagName("TITLE")[0];
          }, function (res) {
         console.log('Admin page title: '+res.text);
        })   
        .run(function(err, nightmare){
          console.log('Done2.');
        })
      ;
    })
 ;

当我使用:node app.js运行此命令时,我能够成功登录到第一页。不幸的是,当我尝试打开第二页时,我看到第二页调用(http://example.com/admin)拒绝访问。 session 没有被带入第二个“goto”命令。

我该怎么做才能使用同一个 nightjs session 打开多个页面?

最佳答案

您是否尝试过链接 goto 方法?

 new Nightmare()
  .goto('http://example.com/user')
    .type('#edit-name', user)
    .type('#edit-pass', pass)
    .click('.searchsubmit')
    .wait()
    .evaluate(function () {
       return document.getElementsByTagName("TITLE")[0];
       }, function (res) {
      console.log('Homepage title: '+res.text);
    })
    .goto('http://example.com/admin')
        .wait()
        .evaluate(function () {
          return document.getElementsByTagName("TITLE")[0];
          }, function (res) {
         console.log('Admin page title: '+res.text);
        })   
        .run(function(err, nightmare){
          console.log('Done2.');
        })
      ;
    }).run();

阅读 api docs run 仅执行之前的命令。

关于javascript - Nightmarejs 同一测试中的多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26726495/

相关文章:

javascript - 如何将从评估返回的变量传递给Nightmare.js中的另一个评估

windows - 如何在 Windows 上更新 npm?

javascript - nightmare.js-$未定义

node.js - .wait() nightmare 和 Puppeteer evaluate 都找不到 ID

javascript - 如何使用 Nightmare js 获取 JavaScript 触发下载?

javascript - vue loader 一个扩展的不同加载器

javascript - 是(或者可以)用 Nodejs http 服务器替代 Apache 或 IIS 等大型服务器

Javascript 多个原型(prototype)函数 - 如何从另一个调用一个

javascript - 在 Ajax 调用之前验证输入

javascript - QuickSort 算法因 stackoverflow 错误而失败