Puppeteer:在页面加载后等待Ajax调用



>SOLUTION:

根据@pguardiario的建议,以下内容拦截Ajax调用并等待其完成:

await page.waitForResponse('http://localhost:58080/', {timeout: 2000})
.catch(() => {console.log('[puppeteer] Error')});

我有一个页面在页面加载后执行Ajax调用,我需要木偶师等待该调用完成。在木偶师中:导航后等待 ajax 调用,接受的答案使用await page.waitForSelector(cssSelector);这在我当前的情况下不起作用,因为 Ajax 调用的成功不会向页面添加任何新元素。这是一个最小的工作示例,其中包含有关实际页面功能的一些注释:

index.html

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" charset="utf-8"></script>
</head>
<body>
</body>
<script charset="utf-8">
$(document).ready(function() {
// render data previously stored in localStorage
console.log('Fetching page');
// in reality it is fetching a JSON API endpoint, but for the purposes of this example, use /
$.get("/")
.fail(function () {
// add an error element to the page
console.log('Error');
})
.done(function () {
// empty the table containing the data and rerender
console.log('Success');
})
.always(function () {
// puppeteer can now continue and check for error
console.log('Done fetching page');
});
});
</script>
</html>

test.js

const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('console', consoleObj => console.log(consoleObj.text()));
await page.goto('http://localhost:58080');
// What to do to wait for the completion of the ajax call done after
// document is ready?
console.log('[puppeteer] Sleeping');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('[puppeteer] Closing');
await browser.close();
});

serve.js

const http = require('http');
const fs = require('fs');
http.createServer(function (request, response) {
fs.readFile('index.html', function(error, content) {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
});
}).listen(58080, 'localhost');

在一个终端中:

node serve.js

在另一个:

node test.js

输出:

[puppeteer] Sleeping
Fetching page
Success
Done fetching page
[puppeteer] Closing

我希望木偶师等待在没有预设超时的情况下打印"完成获取页面"的点。我控制页面本身的源代码,因此任何想法都可以接受,即使是那些需要更改目标服务器的想法。例如,如果我在 Ajax 调用结束后添加一个隐藏的 DOM 元素,我可以使用await page.waitForSelector(cssSelector);.我想知道是否有更好的方法。

我相信木偶师有一个你可以挂钩的加载事件。

所以像这样:

const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
page.on('console', consoleObj => console.log(consoleObj.text()));
page.on('load', await () => { //set listener before you go to the page.
// Now do something...
});
await page.goto('http://localhost:58080');
// What to do to wait for the completion of the ajax call done after
// document is ready?
console.log('[puppeteer] Sleeping');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('[puppeteer] Closing');
await browser.close();
});

最新更新