繁体   English   中英

在 puppeteer 中,如何拦截 window.open 之后的重定向?

[英]In puppeteer, how do you intercept a redirect after 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().legnth legnth 和isNavigationRequest ,因为您不想阻止对页面本身的第一次导航

await page.setRequestInterception(true);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM