alibaba/page-agent · error · Error

Option with text "${optionText}" not found in select element

Error message

Option with text "${optionText}" not found in select element

What it means

selectOptionElement searched all <option> children of the select element and none had textContent matching (trimmed) the provided optionText, so it throws. The match is on the option's visible text, not its value attribute. This guards against silently selecting nothing or the wrong option.

Source

Thrown at packages/page-controller/src/actions.ts:247

	await waitFor(0.1)

	blurLastClickedElement()
}

/**
 * @todo browser-use version is very complex and supports menu tags, need to follow up
 * @private Internal method, subject to change at any time.
 */
export async function selectOptionElement(selectElement: HTMLSelectElement, optionText: string) {
	if (!isSelectElement(selectElement)) {
		throw new Error('Element is not a select element')
	}

	const options = Array.from(selectElement.options)
	const option = options.find((opt) => opt.textContent?.trim() === optionText.trim())

	if (!option) {
		throw new Error(`Option with text "${optionText}" not found in select element`)
	}

	selectElement.value = option.value
	selectElement.dispatchEvent(new Event('change', { bubbles: true }))

	await waitFor(0.1) // Wait to ensure change event processing completes
}

interface ScrollableElement extends Element {
	scrollIntoViewIfNeeded?: (centerIfNeeded?: boolean) => void
}

/**
 * @private Internal method, subject to change at any time.
 */
export async function scrollIntoViewIfNeeded(element: Element) {
	const el = element as ScrollableElement
	if (typeof el.scrollIntoViewIfNeeded === 'function') {

View on GitHub (pinned to d02db1ee7c)

Solutions

  1. Use the exact visible label text from the current options (re-check getSimplifiedHTML or the select's options after updateTree)
  2. For lazy selects, click the select once to trigger loading, re-index, then selectOption with the now-present option text
  3. Trim and case-match your string; avoid passing the value attribute
  4. If exact text is unstable, fall back to clickElement on the desired option after opening the select

Example fix

// before
await controller.selectOption(8, 'US') // option text is 'United States'

// after
await controller.selectOption(8, 'United States')
Defensive patterns

Strategy: validation

Validate before calling

const el = await controller.element(i); if (el instanceof HTMLSelectElement) { const ok = [...el.options].some(o => (o.textContent ?? '').trim() === myText.trim()); if (ok) await controller.selectOption(i, myText) }

Try / catch

try { await controller.selectOption(i, text) } catch (e) { if (String(e).includes('not found in select element')) { /* re-read current option labels and retry with exact text */ } else throw e }

Prevention

When it happens

Trigger: Passing the option's value attribute instead of its label text (value='US' vs text 'United States'); whitespace/casing/nested-markup differences in option text; the option list is populated lazily and not yet loaded; typo in the text.

Common situations: Agent reads the simplified HTML and copies an abbreviated or truncated option label; dynamically populated selects (fetch options after open) where options aren't rendered yet; localization changed option text; option text contains extra whitespace or child span markup.

Related errors


AI-assisted analysis of alibaba/page-agent@d02db1ee7c (2026-08-28). Data as JSON: /api/errors/30d56200acf186d7. Report an issue: GitHub.