Skip to content

Commit

Permalink
feat(playwright): support storage state option
Browse files Browse the repository at this point in the history
  • Loading branch information
idiotWu committed Jan 25, 2025
1 parent 0fb16f8 commit b42b26a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
6 changes: 6 additions & 0 deletions npiai/core/browser/_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BrowserContext,
Page,
FileChooser,
StorageState,
)

__BROWSER_UTILS_VERSION__ = "0.0.15"
Expand Down Expand Up @@ -42,18 +43,21 @@ class PlaywrightContext:
context: BrowserContext | None
page: Page | None
channel: str | None
storage_state: str | pathlib.Path | StorageState | None

def __init__(
self,
headless: bool = True,
channel: str | None = None,
storage_state: str | pathlib.Path | StorageState | None = None,
):
"""
Initialize a Playwright context
Args:
headless: Whether to run playwright in headless mode
channel: The browser channel to use, see: https://playwright.dev/python/docs/browsers#google-chrome--microsoft-edge
storage_state: Previously saved state to use for the browser context
"""
self.headless = headless
self.ready = False
Expand All @@ -62,6 +66,7 @@ def __init__(
self.context = None
self.page = None
self.channel = channel
self.storage_state = storage_state

async def start(self):
"""Start the Playwright chrome"""
Expand All @@ -79,6 +84,7 @@ async def start(self):
self.context = await self.browser.new_context(
locale="en-US",
bypass_csp=True,
storage_state=self.storage_state,
**self.playwright.devices["Desktop Chrome"],
)
# self.context.set_default_timeout(3000)
Expand Down
40 changes: 20 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b42b26a

Please sign in to comment.