Skip to content

fix(browser): drop the empty xpath predicate Capybara adds for exact_text: true - #337

Merged
route merged 1 commit into
mainfrom
fix/exact-text-xpath
Oct 15, 2026
Merged

route merged 1 commit into
mainfrom
fix/exact-text-xpath

Conversation

@route

@route route commented Oct 15, 2026

Copy link
Copy Markdown
Member

Fixes #256.

find_link("Buchen", exact_text: true) raised Capybara::Cuprite::InvalidSelector because the XPath ended in an empty predicate: (.//a[...])[].

On the first attempt Capybara appends its text-match hint as (expr)[...]. With exact_text: true (and no :text), matching_text returns true, xpath_text_conditions returns nil and the predicate ends up empty. Selenium hides it because InvalidSelectorError is in its invalid_element_errors, so synchronize retries without the hint. Adding InvalidSelector to ours would do the same, but every genuinely invalid selector would then retry for the whole wait time before failing.

Instead Browser#find_all drops a trailing [] from XPath selectors. An empty predicate is never valid XPath, so this can't change a valid selector, and nothing is lost since the hint was empty anyway.

The real fix is upstream in teamcapybara/capybara#2860; this workaround can be removed once a release with it is required.

@route
route merged commit b29bb30 into main Oct 15, 2026
7 checks passed
@route
route deleted the fix/exact-text-xpath branch October 15, 2026 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Capybara::Cuprite::InvalidSelector

1 participant