admin管理员组文章数量:1391964
I know how to get the url, I couldn't find much on the page response text however.
await page.on('request', request => {
console.log('INTERCEPTED: ' + request.url());
request.continue();
});
How would I log the page response text?
I know how to get the url, I couldn't find much on the page response text however.
await page.on('request', request => {
console.log('INTERCEPTED: ' + request.url());
request.continue();
});
How would I log the page response text?
Share Improve this question asked Nov 11, 2020 at 16:52 LingeringDogSpongeLingeringDogSponge 671 silver badge7 bronze badges3 Answers
Reset to default 2You have to call setRequestInterception
before binding to request
await page.setRequestInterception(true);
page.on('response', (response) => {
console.log('RESPONSE RECEIVED');
console.log(response.status + ' ' + response.url);
});
page.on('request', request => {
console.log('INTERCEPTED: ' + request.url());
request.continue();
});
await page.setRequestInterception(true);
await page.on('requestfinished', async (request) => {
var response = await request.response();
try {
if (request.redirectChain().length === 0) {
var responseBody = await response.buffer();
console.log(responseBody.toString());
}
}catch (err) { console.log(err); }
});
await page.on('request', request => {
request.continue();
});
response.text() literally will always return null, its another redundant feature of puppeteer however the above works just fine to get the page response which apparently is only present when the request fully loads. ill never get over how many features of puppeteer literally seem to do nothing lol
You can find information about response.text()
here. It could be used like so:
page.on('response', async (response) => {
console.log(await response.text());
});
But the problem might also be with the termination of your script. The response you're looking for might not arrive before the end of your script. Imagine you have something like this:
await page.setRequestInterception(true);
page.on('request', request => {
request.continue();
});
page.on('response', response => {
if (response.url().includes('scripts'))
console.log(response.url());
});
await page.type('#search', 'foo');
await page.click('#send-search');
await context.close();
await browser.close();
then you can't be sure that a url with "scripts" will arrive before the end of your script. If it doesn't, you won't see anything in the console.
If you want to wait for a particular response, and do something with its text, it's better to type:
const res = await page.waitForResponse(response => response.url().includes('scripts'));
console.log(await res.text());
本文标签: javascriptHow do you intercept response text with request interception in puppeteerStack Overflow
版权声明:本文标题:javascript - How do you intercept response text with request interception in puppeteer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744601441a2615090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论