Using Selenium in Python to select a radio button
Question:
I am trying to make a bot that automates booking a study room (Microsoft booking). When I try to select the radio button, it doesn’t work. I tried using id and CSS_SELECTOR. I appreciate any help you can provide.
I want select room C, ID:service_2
room_c_select=driver.find_element(By.ID, "service_2")
room_c_select.click()
driver.find_element(By.CSS_SELECTOR,"input#service_2").click()
driver.find_element(By.CSS_SELECTOR,"[class='right serviceCard']input[type='radio'][id='service_2']")[0].click()
Answers:
You have to click on the small circle, i.e.
driver.find_elements(By.CSS_SELECTOR, 'span.image.icon-circleRegular')[1].click()
Using find_elements()
to locate any specific element isn’t a part of the best practices as demonstrated by the other answer.
As per the html published as a comment, to click on the radio-button associated with the text Group Study Room C you can use the following locator strategy:
driver.find_element(By.XPATH, "//div[@class='name line-clamp' and text()='Group Study Room C']//ancestor::label[1]").click()
I am trying to make a bot that automates booking a study room (Microsoft booking). When I try to select the radio button, it doesn’t work. I tried using id and CSS_SELECTOR. I appreciate any help you can provide.
I want select room C, ID:service_2
room_c_select=driver.find_element(By.ID, "service_2")
room_c_select.click()
driver.find_element(By.CSS_SELECTOR,"input#service_2").click()
driver.find_element(By.CSS_SELECTOR,"[class='right serviceCard']input[type='radio'][id='service_2']")[0].click()
You have to click on the small circle, i.e.
driver.find_elements(By.CSS_SELECTOR, 'span.image.icon-circleRegular')[1].click()
Using find_elements()
to locate any specific element isn’t a part of the best practices as demonstrated by the other answer.
As per the html published as a comment, to click on the radio-button associated with the text Group Study Room C you can use the following locator strategy:
driver.find_element(By.XPATH, "//div[@class='name line-clamp' and text()='Group Study Room C']//ancestor::label[1]").click()