Using Selenium in Python to click/select a radio button
Asked Answered
J

7

25

I am trying to select from a list of 3 buttons, but can't find a way to select them. Below is the HTML I am working with.

<input name="pollQuestion" type="radio" value="SRF"> 
    <font face="arial,sans-serif" size="-1">ChoiceOne</font><br />
<input name="pollQuestion" type="radio" value="COM">
    <font face="arial,sans-serif" size="-1">ChoiceTwo</font><br />
<input name="pollQuestion" type="radio" value="MOT">
    <font face="arial,sans-serif" size="-1">ChoiceThree</font>

I can find it by using the following code:

for i in browser.find_elements_by_xpath("//*[@type='radio']"):
     print i.get_attribute("value")

This outputs: SRF,COM,MOT

But I would like to select ChoiceOne. (To click it) How do I do this?

Janellejanene answered 24/1, 2014 at 0:22 Comment(0)
A
61

Use CSS Selector or XPath to select by value attribute directly, then click it.

browser.find_element_by_css_selector("input[type='radio'][value='SRF']").click()
# browser.find_element_by_xpath(".//input[@type='radio' and @value='SRF']").click()

Corrections (but OP should learn how to look up in documentation)

  • In Python binding, find_elements_by_css doesn't exist, it's called find_elements_by_css_selector. One should be able to look at the exception message and look back into documentation here and figure out why.
  • Notice the difference between find_element_by_css_selector and find_elements_by_css_selector? The first one finds the first matching element, the second one finds a list, so you need to use [0] to index. Here is the API documentation. The reason why I use the latter, is because I copied your code, which I shouldn't.
Alsatia answered 24/1, 2014 at 0:26 Comment(3)
WebDriver has no attribute 'find_elements_by_css'. I would prefer to use CSS, as I am taking it from CSS, but found more success adding [0] to the 2nd entry.Janellejanene
actually it's find_element_by_css_selector("input[type='radio'][value='SRF']").click()Flareup
@Flareup Yes, exactly. I just went ahead and edited the post so that it presents code that actually works.Zanazander
A
4

Enter image description here

Selenium webdriver Radio button click

When i used xpath :

driver.find_element_by_xpath("//input[@id='id_gender2']").click()

radio button not selected

But I used css_selector :

driver.find_element_by_css_selector("input#id_gender1").click() 

radio button selected

Atalanta answered 3/7, 2018 at 6:37 Comment(0)
T
3

find_elements_by_css_selector worked for me,

browser.find_elements_by_css_selector("input[type='radio'][value='SRF']")[0].click()
Theressathereto answered 16/5, 2015 at 13:44 Comment(1)
that works. What I think is even better: Use find_element instead of find_elements. Then you don't get a list back so you don't have to get the element with [0]. So it would be browser.find_element_by_css_selector("input[type='radio'][value='SRF']").click()Pleiad
R
1

First Radio button was not selected for me also. But after inserting Time it works for me.

driver.find_element_by_class_name("login").click()
driver.find_element_by_id("email_create").send_keys("[email protected]")
driver.find_element_by_id("SubmitCreate").click()
time.sleep(2)
driver.find_element_by_css_selector("#id_gender2").click()
Retarded answered 9/8, 2019 at 7:51 Comment(0)
J
1

Consider that you have a radio button to select either of the two options, "Male or "Female". Then try using the following :- This is in Python (Selenium).

driver.find_element_by_xpath("//label[contains(text(),'Male')]").click()

Jayjaycee answered 11/10, 2021 at 18:2 Comment(1)
Welcome to Stack Overflow. Please read How to Answer. OP has provided their own code, and it would be much more helpful if you referred to that code when answering instead of making up your own example.Tera
J
0
browser.find_elements_by_xpath(".//input[@type='radio' and @value='SRF']")[0].click

This ended up being the fix. I was getting errors without the [0] there, that a list does not have a click() attribute (even though there was only 1 match). Thanks for the help user1177636!

Janellejanene answered 24/1, 2014 at 17:24 Comment(1)
In this case, you could have used browser.find_element_by_xpath(".//input[@type='radio' and @value='SRF']").click()Alemanni
S
0

Another simple approach if there's only those radio buttons on the whole page (as in my case),

folder = driver.find_elements(By.XPATH,"//*[@type='radio']")
folder[i].click()

You can give the index of the radio button you want to select and it will be selected. Or you can play with it to get your job done.

Scruggs answered 18/6, 2023 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.