Prompt

How do I automate a browser without writing a ton of brittle code?

Technology / Testing & QA1 observationLast seen Oct 26, 2026

Latest observation

Oct 26, 2026OpenAI APIWeb search: off

If your goal is “automate a browser with minimal brittle code”, the best answer is usually:

1) Use a higher-level automation tool

Instead of driving the browser directly with lots of clicks and XPath selectors, use a tool that gives you:

  • stable selectors
  • automatic waiting
  • built-in retries
  • human-readable scripts
  • page-object / workflow abstractions

Good options:

  • Playwright: usually the best choice today for robust browser automation
  • Selenium: widely used, but often requires more manual waiting and more care
  • Cypress: great for web app testing, less ideal for full browser automation outside its model

If you want the least brittle approach, Playwright is the one I’d start with.


2) Prefer user-facing selectors over implementation details

Brittle automation often comes from targeting:

  • long CSS chains
  • dynamic class names
  • XPath tied to layout
  • text that changes often

Instead, use:

  • data-testid
  • aria-label
  • semantic roles
  • stable IDs

Example in Playwright:

await page.getByRole('button', { name: 'Sign in' }).click();
await page.getByTestId('email').fill('me@example.com');

That’s much more durable than:

await page.click('div > header > nav > ul > li:nth-child(3) > button');

3) Let the tool handle waiting

A lot of brittleness comes from fixed sleeps like:

await page.waitForTimeout(5000);

Avoid that when possible. Use:

  • auto-waiting
  • waitForSelector
  • waiting for navigation
  • waiting for network idle only when appropriate

Playwright handles many waits automatically:

await page.getByRole('button', { name: 'Submit' }).click();
await expect(page.getByText('Success')).toBeVisible();

4) Encapsulate flows into reusable functions

Don’t write one giant script. Make small actions:

  • login
  • search
  • upload file
  • submit form

Example:

async function login(page, email, password) {
  await page.getByLabel('Email').fill(email);
  await page.getByLabel('Password').fill(password);
  await page.getByRole('button', { name: 'Log in' }).click();
}

This makes changes easier to localize when the UI shifts.


5) Use robust app hooks if you control the site

If you can influence the app, add:

  • data-testid
  • accessible labels
  • predictable element structure

This is one of the highest-leverage ways to reduce brittleness.


6) Keep the automation goal narrow

If you automate too much UI, it will be fragile no matter what. Ask:

  • Can this be done via an API instead?
  • Can I use browser automation only for the last-mile UI step?
  • Can I validate state via backend calls instead of the DOM?

Often the least brittle solution is a mix of:

  • API calls for setup
  • browser automation for only what must be done in the UI

7) Run in a clean, repeatable environment

Use:

  • a fresh browser context/profile each run
  • pinned browser versions if possible
  • consistent test data
  • deterministic environment setup

This avoids “works on my machine” problems.


Recommended practical stack

If you want a straightforward, low-brittleness setup:

  • Playwright
  • selectors via roles / labels / test IDs
  • no hard-coded sleeps
  • helper functions for flows
  • use API calls where possible

Tiny example

import { test, expect } from '@playwright/test';

test('logs in', async ({ page }) => {
  await page.goto('https://example.com/login');

  await page.getByLabel('Email').fill('me@example.com');
  await page.getByLabel('Password').fill('secret');
  await page.getByRole('button', { name: 'Log in' }).click();

  await expect(page.getByText('Welcome back')).toBeVisible();
});

If you want, I can also show you:

  1. a Playwright starter template,
  2. how to make automations less flaky,
  3. or how to do this in Python instead of JavaScript.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.