Selenium – XPath for Radio Buttons

XPath for Radio Buttons in Selenium

In this tutorial, we will learn how to build XPath expression for radio buttons based on their attribute values, position, state, etc., with examples in Selenium Python.

1. XPath to locate all the Radio buttons

The XPath expression to locate all the radio buttons in the web page is given below.

xpath_expression = "//input[@type='radio']"

We can use this expression with find_elements() method, to get all the radio button elements.

xpath_expression = "//input[@type='radio']"

radio_buttons = driver.find_elements(By.XPATH, xpath_expression)

2. XPath to locate Radio button by value

The XPath expression to locate a radio button, by the value attribute, in the web page is given below.

xpath_expression = "//input[@type='radio' and @value='value_here']"

We can use this expression with find_element() method, to get the radio button element with given specific value.

xpath_expression = "//input[@type='radio' and @value='value_here']"

radio_button = driver.find_element(By.XPATH, xpath_expression)

Replace 'value_here' with required value of the radio button that you would like to find.

3. XPath to locate Radio button by position

The XPath expression to locate a radio button in the web page by its position or index is given below.

xpath_expression = "(//input[@type='radio'])[index_number]"

We can use this expression with find_element() method, to get the radio button element with given specific value.

xpath_expression = "(//input[@type='radio'])[index_number]"

radio_button = driver.find_element(By.XPATH, xpath_expression)

Replace index_number with required (integer) index value of the radio button that you would like to find. The index starts from one, and increments by one for the subsequent radio buttons.

For example, to get the first radio button, use the following XPath expression.

xpath_expression = "(//input[@type='radio'])[1]"

Refer Python Selenium – Select second radio button tutorial, in which we find the second radio button, based on the position.

4. XPath to locate Radio buttons by name attribute

The XPath expression to locate the radio buttons in the web page by given name attribute is given below.

xpath_expression = "//input[@type='radio' and @name='name_value']"

We can use this expression with find_elements() method, to get the radio buttons with given specific name attribute value.

xpath_expression = "//input[@type='radio' and @name='name_value']"

radio_buttons = driver.find_elements(By.XPATH, xpath_expression)

Replace name_value with required (string value) name attribute value of the radio buttons that you would like to find.

For example, to get the radio buttons whose name attribute is 'fav_subject', use the following XPath expression.

xpath_expression = "//input[@type='radio' and @name='fav_subject']"

5. XPath to locate checked Radio buttons

The XPath expression to locate checked/selected radio buttons is given below.

xpath_expression = "//input[@type='radio' and @checked='checked']"

or

xpath_expression = "//input[@type='radio' and @checked]"

We can use this expression with find_elements() method, to get all the selected radio buttons.

xpath_expression = "//input[@type='radio' and @checked='checked']"

selected_radio_buttons = driver.find_elements(By.XPATH, xpath_expression)

Refer Python Selenium – Get checked radio button tutorial, where we find the selected radio button in a radio group, using the above XPath expression.

6. XPath to locate disabled Radio buttons

The XPath expression to locate disabled radio buttons is given below.

xpath_expression = "//input[@type='radio' and @disabled='disabled']"

We can use this expression with find_elements() method, to get all the disabled radio buttons.

xpath_expression = "//input[@type='radio' and @disabled='disabled']"

disabled_radio_buttons = driver.find_elements(By.XPATH, xpath_expression)

Summary

In this Python Selenium tutorial, we have given instructions on how to build XPath expression to find radio buttons in the webpage, based on attribute value, state, etc.

Related Tutorials

Quiz on Selenium

Q1. Which of the following is not a popular web driver used in Selenium Python?

Not answered

Q2. What is the method used to find web elements in Selenium Python?

Not answered

Q3. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍