Skip to main content

AI Locators for Playwright

By

NPM Version PyPI - Version Twitter Discord GitHub Repo stars

AI-powered selectors for Playwright, available for both Python and Node.js. These packages allow you to use natural language descriptions to locate elements on a webpage using LLM (Large Language Model) technology.

// 👎 Complex XPath with multiple conditions
page.locator("//div[contains(@class, 'header')]//button[contains(@class, 'login') and not(@disabled) and contains(text(), 'Sign In')]");

// 😎 Using ai-locators
page.locator("ai=the login button in the header that says Sign In");

Why?

  • ai-locators do not require maintenance
  • native integration with Playwright

⚠️ Warning: This package is currently experimental and not intended for production use. It may have:

  • Unpredictable behavior
  • Performance overhead from LLM calls
  • Potential security implications

We recommend using it for prototyping and testing purposes only.

Supported Models

ai-locators works with flagship models for now. Smaller models proved not to be powerful enough for the selector generation task.

Model Name Test Badge
Sonnet 3.5 Sonnet 3.5
Sonnet 3.7 Sonnet 3.7
GPT-4o GPT-4o
Google Gemini 2.0 Flash 001 Google Gemini 2.0 Flash 001
Meta LLaMA 3.3 70B Instruct Meta LLaMA 3.3 70B Instruct

Any model with a compatible AI interface can be used with ai-locators, but the models listed above have been thoroughly tested and are known to work well with the package.

Node.js Package

Installation

npm install ai-locators

Usage

const { chromium } = require('playwright');
const { registerAISelector } = require('ai-locators');

const apiKey = process.env.OPENAI_API_KEY;
const baseUrl = process.env.OPENAI_BASE_URL;
const model = "gpt-4o";

(async () => {
  const browser = await chromium.launch({
    headless: false,
    args: ["--disable-web-security"]  // Disable CORS to make LLM request. Use at own risk.
  });
  const page = await browser.newPage();
  
    await registerAISelector({
      apiKey: apiKey,
      model: model,
      baseUrl: baseUrl,
    });
    console.log("Registered AI selector");
  
  // Navigate to a page
  await page.goto("https://playwright.dev/")
  
  // Use the AI selector with natural language
  const element = page.locator("ai=get started button")
  await element.click();
  console.log("Clicked get started button");
  
  await browser.close();
})();

Python Package

Installation

pip install ai-locators

Usage

from playwright.sync_api import sync_playwright
from ai_locators import register_ai_selector

api_key = os.getenv("OPENAI_API_KEY")
base_url = os.getenv("OPENAI_BASE_URL")
model = "gpt-4o"

with sync_playwright() as p:
    # Need to disable web security for browser to make LLM requests work
    browser = p.chromium.launch(headless=False, args=["--disable-web-security"])  # Disable CORS to make LLM request. Use at own risk.
    page = browser.new_page()
    
    # Register the AI selector
    register_ai_selector(p, api_key, base_url, model)
    
    # Navigate to a page
    page.goto("https://playwright.dev/")
    
    # Use the AI selector with natural language
    element = page.locator("ai=get started button")
    element.click()
    
    browser.close()

Custom Prefix

You can customize the prefix used for AI selectors. By default, it's ai=, but you can change it to anything you prefer.

In Node.js

await registerAISelector({
  apiKey: "...",
  baseUrl: "...",
  model: "...",
  selectorPrefix: "find"  // Now you can use "find=the login button"
});

In Python

register_ai_selector(p,
    api_key="...",
    base_url="...",
    model="...",
    selector_prefix="find"  # Now you can use "find=the login button"
)

Plug in your LLM

The packages work with any OpenAI-compatible LLM endpoint. You just need to pass model, api_key and base_url when registering the selector.

For example:

In Node.js

// OpenAI
await registerAISelector({
    apiKey: "sk-...",
    baseUrl: "https://api.openai.com/v1",
    model: "gpt-4"
});

// Anthropic
await registerAISelector({
    apiKey: "sk-ant-...",
    baseUrl: "https://api.anthropic.com/v1",
    model: "claude-3-sonnet-20240229"
});

// Ollama
await registerAISelector({
    apiKey: "ollama",  // not used but required
    baseUrl: "http://localhost:11434/v1",
    model: "llama2"
});

// Basically any OpenAI compatible endpoint

In Python

# OpenAI
register_ai_selector(p, 
    api_key="sk-...",
    base_url="https://api.openai.com/v1",
    model="gpt-4"
)

# Anthropic
register_ai_selector(p,
    api_key="sk-ant-...",
    base_url="https://api.anthropic.com/v1",
    model="claude-3-sonnet-20240229"
)

# Ollama
register_ai_selector(p,
    api_key="ollama",  # not used but required
    base_url="http://localhost:11434/v1",
    model="llama2"
)

# Basically any OpenAI compatible endpoint

How it works

ai-locators uses the custom selector engine feature from Playwright: https://playwright.dev/docs/extensibility Each time a locator needs to be resolved, an LLM call is used to generate the appropriate selector.

Best practices

Narrowing Down Selectors

For better performance and reliability, it's recommended to first locate a known container element using standard selectors, then use the AI selector within that container. This approach:

  • Reduces the search space for the AI
  • Improves accuracy by providing more context
  • Reduces LLM token usage
  • Results in faster element location

Metadata

Release files for ai-locators 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ai-locators 0.1.1
File Size Uploaded
ai_locators-0.1.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ai-locators 0.1.1
File Interpreter ABI Platform
ai_locators-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / ai_locators-0.1.1.tar.gz

Download URL ai_locators-0.1.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
577ba8f59d0f1071253a487dcacebf53a076dadc7d7937967477db9f1650ed17
BLAKE2b-256 checksum
How to use checksums
67eec969c69279ddf94e27c4b4a120e41bdce333ac3be3fa31920fad150a7b81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / ai_locators-0.1.1-py3-none-any.whl

Download URL ai_locators-0.1.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e54f8969e90f70c6339aa6fbca7ae3b81d2e314a2a224fa8d6fe2f52c8b5c983
BLAKE2b-256 checksum
How to use checksums
31d46f3853b16433458f2139159cee263efc25ffb925703f36a660a1de6848ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page