How can I use the selenium to interact with the radio buttons?

267    Asked by Aalapprabhakaran in QA Testing , Asked on Mar 13, 2024

 I am currently engaged in a particular task which is related to testing the registration form which includes a set of radio buttons to select the gender of the users. How can I use the selenium to interact with these radio buttons and how can I verify that the selected gender is successfully submitted? 

Answered by Aryan Tandon

In the context of selenium, you can interact with the radio buttons by using the selenium by using the steps which are given below:-

Locate the radio buttons

You can try to use the selenium to locate the radio button elements on the webpage by using the right locator strategy.

Select the desired radio button

You can use the click() method to select the desired radio button. If the radio buttons are a part of the group, then you would need to click the desired radio button.

Verify the selection

You can optionally verify the correct radio button by clicking its “selected” attribute or you can just assert that its state has changed after the selection.

Here is an example given of how you can interact with the radio buttons by using the selenium in Python programming language:-

From selenium import webdriver

# Instantiate WebDriver
Driver = webdriver.Chrome()
# Navigate to the webpage with the registration form
Driver.get(‘https://example.com’)
# Locate the radio button element for gender selection
Gender_radio = driver.find_element_by_xpath(‘//input[@id=”gender_male”]’)
# Select the radio button for male gender
Gender_radio.click()
# Verify that the male gender radio button is selected
Assert gender_radio.is_selected(), “Male gender radio button is not selected.”
# Optionally, submit the form or perform further actions
# Close the WebDriver
Driver.quit()


Your Answer

Interviews

Parent Categories