-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplaywright_basic.py
45 lines (34 loc) · 1.27 KB
/
playwright_basic.py
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
import os
from playwright.sync_api import Playwright, sync_playwright
from browserbase import Browserbase
BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY", "")
if not BROWSERBASE_API_KEY:
raise ValueError("BROWSERBASE_API_KEY is not set")
BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID", "")
if not BROWSERBASE_PROJECT_ID:
raise ValueError("BROWSERBASE_PROJECT_ID is not set")
bb = Browserbase(
# This is the default and can be omitted
api_key=BROWSERBASE_API_KEY,
)
def run(playwright: Playwright) -> None:
# Create a session on Browserbase
session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
# Connect to the remote session
chromium = playwright.chromium
browser = chromium.connect_over_cdp(session.connect_url)
context = browser.contexts[0]
page = context.pages[0]
# Execute Playwright actions on the remote browser tab
page.goto("https://news.ycombinator.com/")
page_title = page.title()
assert (
page_title == "Hacker News"
), f"Page title is not 'Hacker News', it is '{page_title}'"
page.screenshot(path="screenshot.png")
page.close()
browser.close()
print("Done!")
if __name__ == "__main__":
with sync_playwright() as playwright:
run(playwright)