Playwright can't click Streamlit st.radio options: input[type=radio][value='Model Details'] times out (values are '0','1'), get_by_label().check() times out (0-px input); click the label text
Context: Playwright screenshot tests for a Streamlit 1.40.1 app whose pages are switched with a sidebar st.radio('Page', ['Map & Overview', 'Model Details']). The first screenshot works. Navigation then dies with Locator.click: Timeout 30000ms exceeded on
page.locator('input[type="radio"][value="Model Details"]').click()What Streamlit renders (checked with playwright 1.65.2 in Chromium): the real <input type="radio"> elements carry the option INDEX as their value, not the label: value="0", value="1". They're also visually hidden (position: absolute, 0 px wide, tabindex="-1"). The label text sits in a sibling element inside a <label>. So:
- input[value="Model Details"] matches nothing and waits until the timeout.
- page.get_by_label("Model Details").check() and page.get_by_role("radio", name="Model Details").check() find the input but can't act on a zero-size element. The first times out; with force=True it fails with Element is outside of the viewport.
What works: click the visible label text, scoped to the radio widget:
radio = page.get_by_test_id('stRadio') # data-testid="stRadio"
radio.get_by_text('Model Details', exact=True).click()
page.get_by_text('PAGE=Model Details').wait_for() # wait for the rerun to finishpage.locator('[data-testid="stRadio"] label').filter(has_text='Map & Overview').click() also works. With several radios on a page, narrow it down first by the group's accessible name: page.get_by_role('radiogroup', name='Page') (Streamlit puts the widget label in the radiogroup's aria-label).
Two more Streamlit-specific waits:
- After any widget interaction Streamlit reruns the whole script. Wait for content that only exists on the new page, not for the radio itself, before you take a screenshot.
- On first load, wait for the widget (page.get_by_test_id('stRadio').wait_for()) instead of a fixed sleep. And if the script crashes, the page shows [data-testid="stException"] and no widgets at all, so check for that element and fail fast with its text. Otherwise a broken app (in our case a stale h3 API call) looks like a selector timeout.