承诺在puppeteer内部等待获取请求



我正在尝试用我的凭据登录到一个站点,然后等待页面导航,最后在登录后向端点发送一个post请求。然而,我发现我的承诺总是悬而未决,例如:

下面是一个片段:

await page.goto('https://id.adform.com/sts/login')
await Promise.all([
page.evaluate(() => {
(function() {const setValue = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value").set;
const modifyInput = (name, value) => {
const input = document.getElementsByName(name)[0]
setValue.call(input, value)
input.dispatchEvent(new Event('input', { bubbles: true}))
};
modifyInput('username', "another");
modifyInput('password', "some.123");
document.querySelector("button").click();
}())
}),
page.waitForNavigation({waitUntil: 'networkidle2'}),
]);
await page.evaluate(() => {fetch('https://page_after_navigation.com', {
method: 'POST',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
},
body: JSON.stringify(this.json_data_)
}).then( (response) => {
return response.text();
}).then(function(data) {
console.log(data); // this will be a string
})
})

这将进入页面,填写表单,提交按钮并等待页面导航。然后获取页面请求,其中cookie从页面导航中获取并在获取请求中使用。

据我所知,如果promise是pending的,你在代码实现的某个地方缺少async/await。

最新更新