24

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?

Das Bruno
  • 345
  • 1
  • 3
  • 7

6 Answers6

54

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.
Josh O'Brien
  • 154,425
  • 26
  • 353
  • 447
Yi Zeng
  • 30,504
  • 12
  • 92
  • 119
  • 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 Jan 24 '14 at 17:21
  • 5
    actually it's `find_element_by_css_selector("input[type='radio'][value='SRF']").click()` – sspross Mar 09 '18 at 06:37
  • 1
    @sspross Yes, exactly. I just went ahead and edited the post so that it presents code that actually works. – Josh O'Brien Feb 21 '20 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

alexander.polomodov
  • 5,140
  • 14
  • 38
  • 43
R P
  • 41
  • 2
3

find_elements_by_css_selector worked for me,

browser.find_elements_by_css_selector("input[type='radio'][value='SRF']")[0].click()
praba230890
  • 2,074
  • 19
  • 35
  • 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()` – Micromegas Oct 20 '20 at 02: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("testsel000@gmail.com")
driver.find_element_by_id("SubmitCreate").click()
time.sleep(2)
driver.find_element_by_css_selector("#id_gender2").click()
kavi
  • 11
  • 1
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()

SK_
  • 11
  • 1
  • Welcome to Stack Overflow. Please read [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 Oct 11 '21 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!

Das Bruno
  • 345
  • 1
  • 3
  • 7
  • In this case, you could have used browser.find_element_by_xpath(".//input[@type='radio' and @value='SRF']").click() – Invincible Nov 21 '18 at 10:49