Skip to main content

WebStorage

WebStorage 通过异步、浏览器一致的 API 为当前源公开页面的 localStoragesessionStorage

🌐 WebStorage exposes the page's localStorage or sessionStorage for the current origin via an async, browser-consistent API.

实例可以通过 Page.LocalStoragePage.SessionStorage 访问。

🌐 Instances are accessed through Page.LocalStorage and Page.SessionStorage.

await page.GotoAsync("https://example.com");
await page.LocalStorage.SetItemAsync("token", "abc");
var token = await page.LocalStorage.GetItemAsync("token");
var all = await page.LocalStorage.ItemsAsync();
await page.LocalStorage.RemoveItemAsync("token");
await page.LocalStorage.ClearAsync();

方法

🌐 Methods

ClearAsync

Added in: v1.61 webStorage.ClearAsync

从存储中移除所有物品。

🌐 Removes all items from the storage.

用法

await WebStorage.ClearAsync();

返回


GetItemAsync

Added in: v1.61 webStorage.GetItemAsync

如果存在,返回给定name的值。

🌐 Returns the value for the given name if present.

用法

await WebStorage.GetItemAsync(name);

参数

  • name string#

    要检索的项目名称。

返回


ItemsAsync

Added in: v1.61 webStorage.ItemsAsync

以名称/值对的形式返回存储中的所有项目。

🌐 Returns all items in the storage as name/value pairs.

用法

await WebStorage.ItemsAsync();

返回


RemoveItemAsync

Added in: v1.61 webStorage.RemoveItemAsync

移除具有给定name的项目。如果项目不存在,则不执行任何操作。

🌐 Removes the item with the given name. No-op if the item is absent.

用法

await WebStorage.RemoveItemAsync(name);

参数

  • name string#

    要移除的物品名称。

返回


SetItemAsync

Added in: v1.61 webStorage.SetItemAsync

为指定的 name 设置值。会覆盖该名称的任何现有值。

🌐 Sets the value for the given name. Overwrites any existing value for that name.

用法

await WebStorage.SetItemAsync(name, value);

参数

  • name string#

    要设置的项目名称。

  • value string#

    该项目的新值。

返回