ScreenshotNeo

BlogHow-to

How to Select Radio Buttons with Selenium

Select a radio option with a stable Selenium locator, wait for it to be clickable, and verify it is selected. Includes Python, Java, JavaScript, and troubleshooting.

By the ScreenshotNeo team4 October 20268 min read

To select a radio button with Selenium, locate the specific radio input, wait until it is ready to click, call click(), and verify the result with the binding’s selected-state method. In Python, that method is is_selected(). Use a locator that identifies both the radio group and the desired option, such as its name and value.

For example, given <input type="radio" name="plan" value="premium">, the CSS selector input[name="plan"][value="premium"] identifies that option. Replace these attributes with the stable ones from your page.

1. Identify and locate the intended radio button

Inspect the page markup or DOM to find the radio input and its associated label. Prefer a stable, unique ID when available. Otherwise, use a concise CSS selector based on meaningful attributes. XPath is useful when the relationship between elements makes the target clearer, but avoid brittle absolute paths.

Locator Example Use it when
ID By.ID, "premium-plan" The input has a stable, unique ID.
Name and value input[name="plan"][value="premium"] The radio group and option have meaningful attributes.
CSS class or attribute input.plan-option[value="premium"] The class or attribute is stable and identifies the option.
XPath //input[@name='plan' and @value='premium'] An XPath expression makes the target relationship clearer.

A locator should match the intended option, not just the first radio on the page. Check uniqueness if the page has repeated forms or multiple groups. Selenium supports ID, name, CSS selector, XPath, and other locator strategies; its locator guidance recommends a unique, predictable ID where one is available, then a well-written CSS selector. See Selenium’s locator guide and locator recommendations.

2. Select and verify a radio button in Python

This runnable example uses Selenium Manager to obtain a compatible browser driver when needed. Install Selenium with python -m pip install selenium. Save as select_radio.py and run with python select_radio.py. Set FORM_URL to a page you can access that contains the example radio group, or replace the URL and selector with those for your application.

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

url = os.environ.get("FORM_URL", "https://example.com/form")
selector = 'input[name="plan"][value="premium"]'

driver = webdriver.Chrome()
try:
    driver.get(url)
    wait = WebDriverWait(driver, 10)
    radio = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)))

    if not radio.is_selected():
        radio.click()

    assert radio.is_selected(), "Expected the premium radio option to be selected"
finally:
    driver.quit()

The example URL is a placeholder; it does not claim that example.com/form contains this form. Use your actual page URL and matching selector. The state check before clicking makes the action safe to repeat when the option may already be selected. Python’s WebElement.is_selected() is the documented selected-state API for radio buttons and checkboxes; see the Python WebElement API.

3. Other Selenium language bindings

Java

With Selenium’s Java binding, use isSelected() (camel case). The sample assumes the driver has been configured and the page loaded; supply the actual URL and selector.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

WebDriver driver = new ChromeDriver();
try {
    driver.get("https://example.com/form");
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    By target = By.cssSelector("input[name='plan'][value='premium']");
    WebElement radio = wait.until(ExpectedConditions.elementToBeClickable(target));

    if (!radio.isSelected()) {
        radio.click();
    }
    if (!radio.isSelected()) {
        throw new AssertionError("Expected premium option to be selected");
    }
} finally {
    driver.quit();
}

JavaScript (Node.js)

Install the JavaScript binding with npm install selenium-webdriver. This example uses Chrome and the JavaScript binding’s isSelected() method.

const { Builder, By, until } = require('selenium-webdriver');

(async () => {
  const driver = await new Builder().forBrowser('chrome').build();
  try {
    await driver.get('https://example.com/form');
    const radio = await driver.wait(
      until.elementLocated(By.css('input[name="plan"][value="premium"]')),
      10000
    );
    await driver.wait(until.elementIsVisible(radio), 10000);
    await driver.wait(until.elementIsEnabled(radio), 10000);

    if (!(await radio.isSelected())) {
      await radio.click();
    }
    if (!(await radio.isSelected())) {
      throw new Error('Expected premium option to be selected');
    }
  } finally {
    await driver.quit();
  }
})().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

These examples use the standard Selenium WebDriver interaction and selected-state APIs. Adapt setup and imports to the Selenium binding version and browser used by your project.

4. Labels, radio groups, and preselected options

Radio inputs with the same name in a form act as a group: selecting one option deselects another in that group. Unlike checkboxes, radios represent one choice from a set. An input may already be selected when the page loads, so assert the desired final state rather than assuming the click caused a change. See MDN’s radio input reference.

When the input is visually hidden or too small to interact with, click its associated label. A label is associated either through a matching for/id pair or by wrapping the input. For example:

<input type="radio" id="premium" name="plan" value="premium">
<label for="premium">Premium</label>

In Python, wait for the label and click it, then verify the input:

label = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'label[for="premium"]')))
label.click()
radio = driver.find_element(By.ID, "premium")
assert radio.is_selected()

Clicking a label works only when it is correctly associated with the intended input and is itself interactable. If the label is obscured or the association is missing, fix the page markup or choose a valid interaction target rather than forcing a click.

5. Wait for dynamic controls and verify the outcome

Page navigation can finish before JavaScript inserts a radio group or makes it interactable. Wait for the condition your next action needs. In Python, element_to_be_clickable waits until the located element is visible and enabled. If the element is inserted later, the wait repeatedly evaluates the locator until the condition is satisfied or times out.

Use explicit waits for specific conditions. Selenium warns, “Do not mix implicit and explicit waits.” Combining them can produce unpredictable wait durations. Fixed sleeps are also a poor default: they can be too short on a slow page and waste time on a fast one. Read Selenium’s waiting strategies for the binding-specific conditions and guidance.

A reliable interaction sequence is:

  1. Wait until the correct radio is present and interactable.
  2. Check whether it is already selected.
  3. Click only if it is not selected.
  4. Wait for or assert the selected state.

6. Troubleshooting common failures

Symptom Likely cause Fix
NoSuchElementException The selector is wrong, the page is not the expected one, or the control has not rendered. Inspect the live DOM, confirm the group and option attributes, and wait for the relevant element.
More than one element matches The locator targets a group or repeated component rather than one option. Add stable attributes such as the group name and option value, or scope the search to the correct form.
Click intercepted or not clickable An overlay, animation, or another element covers the radio; it may also be hidden. Wait for the obstruction or transition to end. If the visible label is the intended control, click its associated label and verify the input state.
Click succeeds but the expected radio is not selected The wrong option was located, the page rerendered the group, or application logic changed the value. Recheck the selector and group name, then wait for the final selected state after the interaction.
Timeout waiting for clickability The input never becomes visible/enabled, a condition is too strict, or the page is in an unexpected state. Check visibility and enabled state in the browser, inspect validation or loading behavior, and wait on the actual prerequisite.
Wait takes much longer than expected Implicit and explicit waits are combined, or the wait condition does not match the page state. Use one consistent wait strategy and a condition tied to the target control.
Element becomes stale A rerender replaced the element after it was found. Wait for the rerender to finish and locate the element again before clicking or asserting.

Avoid using JavaScript to force a click as the first fix. It can bypass normal user interaction and conceal a locator, timing, or overlay problem that the test should reveal.

7. Performance, reliability, and test cost

Radio selection is a small browser interaction; most time and flakiness usually come from page startup, network activity, rendering, and synchronization. Use a specific selector and wait for the relevant condition instead of adding a long fixed delay. Keep the selected-state assertion: it makes the test fail close to the interaction if the wrong option remains active.

For reliable automated tests, isolate test data and start from a known page state. Account for whether the target is preselected, whether the page rerenders after a choice, and whether validation or dependent content changes the form. Always close the driver in a cleanup block so browser processes do not accumulate after failures. No benchmark or runtime guarantee is implied by these practices.

8. Or skip the browser setup

If your task is to inspect or capture how a page looks rather than exercise its radio controls, ScreenshotNeo can return a website screenshot with one GET request. It is a screenshot API and MCP server; it does not select radio buttons or replace Selenium interaction tests.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. Cookie banners are accepted like a visitor and more than 60 known consent platforms, newsletter popups, and chat widgets are removed before capture; each step can be turned off. Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify the page verdict and billing status in headers. An MCP server lets Claude, Cursor, and other MCP clients use screenshot tools. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up free for 1,000 screenshots a month, with no card required.

9. FAQ

Does Selenium have a special command for radio buttons?

No. Locate the radio as a WebElement, click it, and check its selected state using the method provided by your language binding.

How do I select a radio option by its displayed text?

Prefer locating its associated label by a stable attribute such as its for value, click that label, and verify the corresponding input. Matching visible text can be useful when no stable label attribute exists, but may be ambiguous across forms or locales.

Should I click an already-selected radio?

Usually there is no need. Check the selected state first, then click only when the desired option is not selected. Always assert the final state.

Can I select multiple radios with the same name?

Only one option in a radio group can be selected at a time. Selecting another option in that group replaces the current choice.