Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1· openaiHugging Face Frontier Reasoning Traces

Develop a Python script to automate browser actions with Playwright.

  1. User

    Develop a Python script to automate browser actions with Playwright.

  2. Assistant

    Developing browser automation script

    The user is asking for a Python script using Playwright to automate browser actions, but they haven't provided specifics. I think they likely want a concise, info-packed template that covers installation, basic actions like opening a page, clicking, filling forms, and handling different modes. I should probably use the synchronous Playwright API since it's simpler. I can provide installation instructions, a basic script, and explanations, including how to run it with CLI parameters. I'll aim for a 60-80 line code response with some context!Designing automation script

    I'm crafting a Python script for Playwright without using emojis. First, I'll include installation commands for Playwright. The initial script sets up a browser, navigates to a webpage, fills out a search form, and takes a screenshot. However, I realize that using example.com isn't ideal since it lacks the necessary elements.

    Instead, I’ll use Wikipedia for search, showing proper selectors for page interactions. I might also add a snippet for automated login. Overall, I want to ensure the script is straightforward and suitable for new users.Proposing automation script structure

    The user wants a Python script for browser automation with Playwright, but it shouldn’t be overly detailed or step-by-step. I'll structure my response into clear sections: first, installation of dependencies, followed by the actual script named automation.py, where I'll set up a main function.

    Next, I can create an example function, search_wikipedia(query: str), and include optional CLI arguments for the search query and headless mode. Since Playwright uses asynchronous by default, I’ll stick to the synchronous API for simplicity.Crafting Playwright automation script

    I’m putting together a minimal Playwright script named automation.py. It will open a webpage, execute a search, click on a result, fill out forms, and take a screenshot. I’ll use the synchronous API to keep things straightforward.

    While using "Playwright (software)" as the search term, I consider that the link might not always be visible. Thus, I could opt for the newer locator methods like page.get_by_role(). However, to keep it simple for now, I’ll stick with the existing link text. It's essential to ensure the CSS selectors are accurate.Creating robust Playwright example

    I’m putting together a robust example using Playwright's demo at https://demo.playwright.dev/todomvc. This script will open the page, add todos, mark one as complete, and take a screenshot. I'll ensure to use standard selectors for clarity.

    I also plan to incorporate command-line arguments for headless mode using argparse. The final answer will structure quick installation steps, provide the automation.py script with clear comments, and briefly touch on customizing selectors. I want to keep everything simple but informative! Here’s a minimal but complete example of using Playwright in Python to automate browser actions.


    1. Install Playwright (one-time setup)

    pip install playwright
    playwright install
    

    playwright install downloads the browser binaries (Chromium, Firefox, WebKit).


    2. Example automation script (Python + Playwright)

    This script:

    • Opens Chromium
    • Navigates to a demo TODO app
    • Adds some TODO items
    • Marks one as complete
    • Clicks a filter
    • Takes a screenshot
    • Closes the browser

    Save as automation.py:

    #!/usr/bin/env python3
    
    from playwright.sync_api import Playwright, sync_playwright
    
    
    def run(playwright: Playwright, headless: bool = False) -> None:
        # Launch browser
        browser = playwright.chromium.launch(
            headless=headless,
            slow_mo=200,  # slow down actions so you can see them
        )
        context = browser.new_context()
        page = context.new_page()
    
        # Go to the demo app
        page.goto("https://demo.playwright.dev/todomvc", wait_until="networkidle")
    
        # Add TODO items
        todo_input = page.get_by_placeholder("What needs to be done?")
        for text in ["Buy milk", "Learn Playwright", "Write automation script"]:
            todo_input.fill(text)
            todo_input.press("Enter")
    
        # Mark first item as completed
        page.locator(".todo-list li").first.locator(".toggle").check()
    
        # Click the "Completed" filter link
        page.get_by_role("link", name="Completed").click()
    
        # Take a screenshot of the page
        page.screenshot(path="todomvc.png", full_page=True)
    
        # Clean up
        context.close()
        browser.close()
    
    
    if __name__ == "__main__":
        with sync_playwright() as playwright:
            # Set headless=True to run without opening a window
            run(playwright, headless=False)
    

    Run it:

    python automation.py
    

    You should see the browser open, actions performed, and a todomvc.png screenshot created.


    3. Adapting this to your own site

    To automate your target site, change the middle part:

    page.goto("https://your-site.example/")
    page.fill("css-or-other-selector", "some value")
    page.click("css-or-other-selector")
    

    You’ll commonly use:

    • page.goto(url)
    • page.fill(selector, text)
    • page.click(selector)
    • page.get_by_role(...), page.get_by_text(...), page.get_by_placeholder(...)
    • page.screenshot(path="file.png")

    If you tell me what site and what actions you want (e.g., login, form submit, file download), I can tailor the script to that workflow.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.