【发布时间】:2020-05-30 09:19:39
【问题描述】:
解决方案:
根据@pguardiario 的建议,以下拦截Ajax 调用并等待其完成:
await page.waitForResponse('http://localhost:58080/', {timeout: 2000})
.catch(() => {console.log('[puppeteer] Error')});
我有一个页面在页面加载后执行 Ajax 调用,我需要 puppeteer 等待该调用完成。在puppeteer : wait for ajax call after navigation 中,接受的答案使用await page.waitForSelector(cssSelector);,这在我目前的情况下不起作用,因为Ajax 调用的成功不会向页面添加任何新元素。这是一个关于实际页面功能的最小工作示例,其中包含一些 cmets:
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
我希望 puppeteer 在没有预设超时的情况下等待打印“完成获取页面”的点。我控制页面本身的源代码,因此接受任何想法,即使是那些需要更改目标服务器的想法。例如,如果我在 Ajax 调用结束后添加一个隐藏的 DOM 元素,我可以使用await page.waitForSelector(cssSelector);。我想知道是否有更好的方法。
【问题讨论】:
-
听起来 waitForResponse 就是你要找的东西。
-
谢谢@pguardiario 这确实是我需要的!
-
如果您发布答案我可以接受
-
YW。我会让你这样做,只需发布你最终得到的任何代码。
标签: javascript node.js ajax puppeteer