Building a ‘Seeing’ AI Agent: Combining OmniParser and Playwright to Completely Bypass CSS Selectors

Artificial Intelligence tutorial - IT technology blog
Artificial Intelligence tutorial - IT technology blog

The Nightmare of ‘Broken Selectors’

Have you ever woken up at 2 AM because an automation script failed just because the frontend changed a class name from .btn-submit to .btn-primary-v2? With modern applications like Salesforce or SAP Web, constantly changing DOM structures make maintaining XPath or CSS Selectors a never-ending battle.

Traditional tools suffer from a fatal flaw: they are “blind.” They interact with source code instead of looking at the interface like a human does. When you see a “Checkout” button, you recognize it by its color, position, and text. You don’t care if its ID is #ext-gen-1024.

To solve this once and for all, I switched to using OmniParser by Microsoft. This is how to help an AI Agent “see” screenshots, understand UI components, and command Playwright to operate without touching a single line of HTML code.

OmniParser: The Eyes of the AI Agent

OmniParser is more than just standard OCR. It is a specialized computer vision model that structures every pixel on the screen into machine-understandable data. Specifically, it performs three main tasks:

  • Detection: Automatically identifies icons, buttons, and input fields with high precision.
  • Functional Semantics: Understands the meaning of elements (e.g., recognizing a cart icon as “Cart”).
  • Coordinate Mapping: Extracts precise (x, y) coordinates for Playwright to click.

When combined with vision-capable LLMs like GPT-4o, OmniParser acts as an information filter. It reduces massive image data into lightweight labels, preventing the AI from getting “overwhelmed” when looking at a complex dashboard.

The System’s Operational Workflow

Instead of hard-coding, my Agent operates in a reasoning loop:

  1. Screenshot: Playwright captures the current screen.
  2. Analysis: OmniParser numbers each button and extracts coordinates.
  3. Decision Making: The LLM receives the numbered image, compares it with the user’s request, and selects the number to interact with.
  4. Execution: Playwright clicks the coordinates corresponding to that number.

Practical Implementation Guide

1. Environment Setup

OmniParser requires a GPU for optimal processing speed. You should run it as a microservice so that your Playwright scripts can call it via an API.

# Clone repo and set up environment
git clone https://github.com/microsoft/OmniParser.git
cd OmniParser
pip install -r requirements.txt

In practice, I usually wrap OmniParser in Docker and deploy it to a server with a GPU (like an RTX 3060 or A100) to process images in under 1 second.

2. Connecting Playwright with Computer Vision

Here is how I control the browser using coordinates instead of traditional selectors:

from playwright.sync_api import sync_playwright
import requests

def get_ui_map(screenshot_path):
    # Send image to OmniParser API
    with open(screenshot_path, "rb") as f:
        response = requests.post("http://localhost:8000/parse", files={"file": f})
    return response.json() 

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto("https://example-dashboard.com")
    
    # Capture screenshot and analyze
    page.screenshot(path="screen.png")
    elements = get_ui_map("screen.png")
    
    # Get coordinates for the 'Export' button recognized by OmniParser
    target = elements['Export_Button']
    page.mouse.click(target['x'], target['y'])
    print(f"Clicked at coordinates {target['x']}, {target['y']}")

3. Prompting Techniques for Vision Agents

Don’t send raw images to the LLM. Send images with numbered bounding boxes drawn by OmniParser. An effective prompt would be: “In this image, what number is assigned to the ‘Add to Cart’ button? Return in JSON format: {‘action’: ‘click’, ‘element_number’: X}”.

Lessons Learned from Real-World Projects

I implemented this system to crawl data from 50 different e-commerce websites. Here are the key numbers and considerations:

  • Stability Rate: Scripts no longer break when the UI changes slightly, reducing selector maintenance time by 80%.
  • Latency: The entire process takes about 3-6 seconds per step. This is a trade-off for flexibility, so it’s not suitable for tasks requiring millisecond speeds.
  • Viewport Considerations: You must set device_scale_factor: 1 in Playwright. Otherwise, the click coordinates on the image and the actual browser coordinates will be misaligned.
  • Cost Optimization: Only call OmniParser when the page undergoes a major change or after a critical action is performed.

Why is This Method the Future?

Breaking away from the DOM is a mandatory step if you want to build self-learning AI Agents. I once tested an Agent on a legacy internal application from 2010 where the HTML code was extremely messy. The result was surprising: the Agent still clicked the correct buttons thanks to visual recognition—something traditional Selenium scripts would have taken hours to debug.

Combining Playwright and OmniParser is not just a new technique. It’s a shift in mindset: teaching computers to see the world the way humans do.

Share: