在木偶师中,如何在window.open之后拦截重定向



假设有一个网站包含以下内容:

<script type="text/javascript">
const onClick = async (event) => {
var page = window.open('', '_blank');
var url = await someCrypticFunctionality();
page.location.replace(url);
}
</script>
<button type="button" onclick="onClick">Click here</button>

使用puppeteer,我想访问页面,点击按钮(到目前为止还不错(,然后检索url,最好是在请求该URL上的任何内容之前。我该怎么做?

我认为this.request.isNavigationRequest更适合您的情况。但是您可能希望将.redirectChain().legnthisNavigationRequest组合起来,因为您不想阻止页面本身的第一次导航

await page.setRequestInterception(true);
page.on("request", async request => {
if (request.isNavigationRequest() && request.redirectChain().length > 0) {
await request.abort();
}
});

最新更新