【发布时间】:2017-07-11 16:58:14
【问题描述】:
我得到了一个 javascript 函数,我需要将其注入到页面中,以便获取稍后将使用的值列表。我可以使用 Chrome 控制台直接在网页上调用此函数,但我想在当前加载的网页上的 nightmareJS 中复制我在 Chrome 控制台中所做的操作。
这是函数:
function getList() {
require(['Service/List'],
function (Service)
{
Service.getList
({
onComplete: function (listOfServices)
{
console.log('List:success:' + JSON.stringify(listOfServices));
},
onFailure: function (error)
{
console.log('List:error:' + error);
}
});
});
}
getList();
我尝试注入文件,但没有成功,我还尝试向该函数添加额外代码以将输出写入文件,但我认为它根本没有被调用。
这里是噩梦JS
describe('Open login page', () => {
it('Login', done => {
nightmare
.goto('http://loginURL.com')
.wait('input[type="text"]')
.wait('input[type="password"]')
.insert('input[type="text"]', 'username')
.insert('input[type="password"]', 'password')
.click('input[type="submit"]')
.evaluate(function() {
nightmare.inject('js', 'getList.js' )
})
//.inject('js', 'getList.js' )
.then(function() {
console.log('Done');
})
})
}) })
这是将javascript文件注入页面后的示例输出:
List:success:"Test":"https://someURL.com/resource/test","Design":"https://someURL.com/resource/Design"},"NewSpace":"https://someURL.com/resource/NewSpace","Generator":"https://Generator.someURL.com/resource/test","SomethingElse":"https://someURL.com/SomethingElse/test","Connection":"https://someURL.com/Connection/test","WorldWide":"https://someURL.com/resource/WorldWide","Vibes":"https://Vibes.someURL.com/resource/test","GoogleSearch":"https://someURL.com/resource/GoogleSearch",
我希望能够通过调用页面上的 javascript 文件来获取该输出并将其保存到一个文件中,以便以后可以使用它来调用该列表中的其他服务。
【问题讨论】:
-
我认为注入的范围与网页上的脚本不同。作为一种解决方法,您可能需要手动注入脚本(到 元素中。如果这种方法适合您,请提供示例代码。
-
我认为这可能有效,但我现在愿意尝试任何事情。如果我能看到一些示例代码,那就太好了!
标签: javascript node.js web-scraping nightmare