Skip to content

Latest commit

 

History

History

04-the-agent-s-hands

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Lesson 4 · The agent's hands

▶ Watch this lesson (starts at 7:47)

Goal

Lesson four. The agent's hands.

Commands and files (as shown in the video)

browser.py · code browser init

from playwright.sync_api import sync_playwright

MAX_CHARS = 20000


class Browser:
    def __init__(self, headless=True):
        self.playwright = sync_playwright().start()
        self.browser = self.playwright.chromium.launch(headless=headless)
        self.context = self.browser.new_context(
            viewport={"width": 1280, "height": 800},
            record_video_dir="videos",
            record_video_size={"width": 1280, "height": 800},
        )
        self.page = self.context.new_page()
        self.page.set_default_timeout(5000)

    def look(self):
        snapshot = self.page.locator("body").aria_snapshot()
        if len(snapshot) > MAX_CHARS:
            snapshot = snapshot[:MAX_CHARS] + "\n... (page cut here)"
        return f"URL: {self.page.url}\nTitle: {self.page.title()}\n\n{snapshot}"

browser.py · code browser actions

from playwright.sync_api import sync_playwright

MAX_CHARS = 20000


class Browser:
    def __init__(self, headless=True):
        self.playwright = sync_playwright().start()
        self.browser = self.playwright.chromium.launch(headless=headless)
        self.context = self.browser.new_context(
            viewport={"width": 1280, "height": 800},
            record_video_dir="videos",
            record_video_size={"width": 1280, "height": 800},
        )
        self.page = self.context.new_page()
        self.page.set_default_timeout(5000)

    def look(self):
        snapshot = self.page.locator("body").aria_snapshot()
        if len(snapshot) > MAX_CHARS:
            snapshot = snapshot[:MAX_CHARS] + "\n... (page cut here)"
        return f"URL: {self.page.url}\nTitle: {self.page.title()}\n\n{snapshot}"

    def go_to(self, url):
        self.page.goto(url)
        return self.look()

    def click(self, role, name):
        self.page.get_by_role(role, name=name, exact=True).first.click()
        return self.look()

    def type_text(self, label, text):
        self.page.get_by_label(label, exact=True).fill(text)
        return self.look()

    def choose(self, label, option):
        self.page.get_by_label(label, exact=True).select_option(label=option)
        return self.look()

    def close(self):
        self.context.close()
        self.browser.close()
        self.playwright.stop()

Terminal · term hands

uv run hands.py
ls videos

Recap

So: four tools, each one returns the new page, and every run leaves a video. Now let's put the model in charge.