我正在尝试从 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/