-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaywright_example.py
More file actions
76 lines (60 loc) · 2.34 KB
/
Copy pathplaywright_example.py
File metadata and controls
76 lines (60 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
"""
Example showing how to use BrowserState with Playwright to maintain browser state
between runs using local storage.
"""
import asyncio
from browserstate import BrowserState, BrowserStateOptions
async def main():
"""Main function demonstrating BrowserState with Playwright"""
try:
from playwright.async_api import async_playwright
except ImportError:
print("Playwright not installed. Install with: pip install playwright")
print("Then install browsers with: python -m playwright install")
return
# Initialize BrowserState with local storage
user_id = "demo_user"
options = BrowserStateOptions(user_id=user_id)
browser_state = BrowserState(options)
# List existing states
states = browser_state.list_states()
print(f"Available states: {states}")
# Mount a state (reuse existing or create new)
state_id = states[0] if states else None
state = browser_state.mount_state(state_id)
print(f"Mounted state: {state['id']} at {state['path']}")
# Use Playwright with the mounted browser profile
async with async_playwright() as p:
# Launch browser with user data directory
browser = await p.chromium.launch_persistent_context(
user_data_dir=state["path"],
headless=False,
)
page = await browser.new_page()
await page.goto("https://example.com")
print("Page loaded, you can interact with the browser")
# Set localStorage value
await page.evaluate("""() => {
localStorage.setItem('browserstate_demo',
JSON.stringify({timestamp: new Date().toISOString()}))
}""")
# Read localStorage value
storage_value = await page.evaluate(
"() => localStorage.getItem('browserstate_demo')"
)
print(f"localStorage value: {storage_value}")
print("Browser open for 30 seconds. Press Ctrl+C to close earlier.")
try:
await asyncio.sleep(30)
except KeyboardInterrupt:
print("Closing browser...")
await browser.close()
# Save the state
browser_state.unmount_state()
print(f"State saved: {state['id']}")
# List states again
states = browser_state.list_states()
print(f"Available states: {states}")
if __name__ == "__main__":
asyncio.run(main())