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?

    7 Answers 7

    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.
    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.
      – Das Bruno
      CommentedJan 24, 2014 at 17:21
    • 5
      actually it's find_element_by_css_selector("input[type='radio'][value='SRF']").click()
      – sspross
      CommentedMar 9, 2018 at 6:37
    • 1
      @sspross Yes, exactly. I just went ahead and edited the post so that it presents code that actually works.CommentedFeb 21, 2020 at 16:56
    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

      3

      find_elements_by_css_selector worked for me,

      browser.find_elements_by_css_selector("input[type='radio'][value='SRF']")[0].click() 
      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()CommentedOct 20, 2020 at 2:18
      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() 
        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()

        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.
          – Chris
          CommentedOct 11, 2021 at 18:04
        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!

        1
        • In this case, you could have used browser.find_element_by_xpath(".//input[@type='radio' and @value='SRF']").click()CommentedNov 21, 2018 at 10:49
        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.

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.