如何在 puppeteer 中禁用缓存? [英] How can I disable cache in puppeteer?

查看:167
本文介绍了如何在 puppeteer 中禁用缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 puppeteer 中禁用缓存,谁能告诉我我该怎么做?我找到了这个 page.setCacheEnabled(enabled) 但我不明白如何使用它.

I want to disable cache in puppeteer, can anyone please tell me how I can do so? I found this page.setCacheEnabled(enabled) but I couldn't understand how to use the same.

我知道浏览器是在没有缓存或 cookie 的情况下启动的,但就我而言,浏览器始终在后台运行,因此需要不同的解决方案.

I am aware that the browser is launched without cache or cookies but in my case the browser is always running in the background thus need a different solution.

推荐答案

根据 puppeteer 文档,您可以使用 await page.setCacheEnabled(enabled)

According to the puppeteer docs you can use await page.setCacheEnabled(enabled)

这是在 12 月添加回来的.参见 Git Hub 问题 #1609

This was added back in December. See Git Hub issue #1609

如果您查看 提交更改例如有一个测试

If you look at the commit changes there is a test e.g.

await page.goto(SOMEURL);

await page.reload({waitUntil: 'networkidle2'});
expect(responses.get('one-style.css').fromCache()).toBe(true);

await page.setCacheEnabled(false);
await page.reload({waitUntil: 'networkidle2'});
expect(responses.get('one-style.css').fromCache()).toBe(false);

这篇关于如何在 puppeteer 中禁用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆