gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:33:55 24 4
gpt4 key购买 nike

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

到目前为止,这是我的代码:

// get the <title> text from inside the Drupal site
var Nightmare = require('nightmare');
var user = 'foobar@example.com';
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”命令。

我该怎么做才能使用相同的 nightmarejs 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com