-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaywright_connect.py
More file actions
77 lines (60 loc) · 2.19 KB
/
playwright_connect.py
File metadata and controls
77 lines (60 loc) · 2.19 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
77
"""
Connect Playwright to a running Multilogin profile over CDP.
Steps:
1. python start_profile.py (in another terminal, or start profile in app)
2. Copy CDP URL from output into MULTILOGIN_CDP_URL
3. pip install playwright && playwright install chromium
4. python playwright_connect.py
"""
import os
import sys
from load_env import bootstrap_env
from multilogin_client import MultiloginClient, MultiloginError
bootstrap_env()
def main() -> None:
cdp_url = os.environ.get("MULTILOGIN_CDP_URL", "")
profile_id = os.environ.get("MULTILOGIN_PROFILE_ID", "")
try:
from playwright.sync_api import sync_playwright
except ImportError:
print("Install: pip install playwright && playwright install chromium", file=sys.stderr)
sys.exit(1)
client = None
started_here = False
if not cdp_url and profile_id:
try:
client = MultiloginClient()
print(f"Starting profile {profile_id} for Playwright...")
session = client.start_profile(profile_id)
cdp_url = client.extract_cdp_url(session) or ""
started_here = True
print("Start response:", session)
except MultiloginError as exc:
print(f"Error: {exc}", file=sys.stderr)
sys.exit(1)
if not cdp_url:
print(
"Set MULTILOGIN_CDP_URL (from start_profile output) or "
"MULTILOGIN_PROFILE_ID to auto-start.",
file=sys.stderr,
)
sys.exit(1)
print(f"Connecting Playwright to {cdp_url}...")
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(cdp_url)
if browser.contexts:
context = browser.contexts[0]
page = context.pages[0] if context.pages else context.new_page()
else:
context = browser.new_context()
page = context.new_page()
page.goto("https://browserleaks.com/javascript")
title = page.title()
print(f"Page title: {title}")
browser.close()
if started_here and client and profile_id:
print(f"Stopping profile {profile_id}...")
client.stop_profile(profile_id)
print("Done.")
if __name__ == "__main__":
main()