0

Using Selenium in Python3 to select a radio button image here. And below is the code I'm using to select the radio button but it doesn't work.

    emailElem = browser.find_element_by_class_name('SSR_DUMMY_RECV1$sels$0')
    emailElem.click()
Carolyn
  • 9
  • 3
  • what does "doesn't work" mean? Does the program crash? Do you get an error message? Does it click the wrong element? – Bryan Oakley Jul 12 '16 at 19:37
  • @BryanOakley Sorry about the ambiguity, I get errors like this ./fsu.py Traceback (most recent call last): File "./fsu.py", line 20, in emailElem.click() AttributeError: 'list' object has no attribute 'click' – Carolyn Jul 13 '16 at 14:43
  • If you get "'list' object has no attribute click", that sounds like you're calling 'find_elements_by_whatever' (notice the "s"). That returns a list of elements rather than a single element. Are you certain you are calling "find element_*" and not "find_elements_*"? Can you please show the exact code and the exact error by editing your question? – Bryan Oakley Jul 13 '16 at 14:55

3 Answers3

2

"by class name" locator should be used to locate elements by class attribute values, in this case use "by id" or "by name" instead:

emailElem = browser.find_element_by_id('SSR_DUMMY_RECV1$sels$0')
emailElem = browser.find_element_by_name('SSR_DUMMY_RECV1$sels$0')
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thank you for responding! Unfortunately I got this error: Traceback (most recent call last): File "./fsu.py", line 20, in emailElem.click() AttributeError: 'list' object has no attribute 'click' – Carolyn Jul 13 '16 at 14:41
  • @user6581117 you are using `find_elements_by_*` but need to use `find_element_*` method - watch the `s`. – alecxe Jul 13 '16 at 14:45
  • thank you for pointing that out. I corrected it but still continue to receive this error. 'selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"name","selector":"SSR_DUMMY_RECV1$sels$0"}' – Carolyn Jul 13 '16 at 14:57
0

Just add a bit more to alecxe's answer.

You need to be sure that its id and name are unique.

If they are not unique, you can add more attributes to find by methods, e.g.

emailElem = browser.find_element_by_css_selector("input[id='SSR_DUMMY_RECV1$sels$0'][class=''PSRADIOBUTTON][type='radio']")

The more attributes you use, the more likely you will locate a unique element.

Yu Zhang
  • 2,037
  • 6
  • 28
  • 42
  • Thank you for responding! Unfortunately I continue to get this error raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: The given selector input[id='SSR_DUMMY_RECV1$sels$0'][class=''PSRADIOBUTTON][type='radio'] is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: An invalid or illegal selector was specified – Carolyn Jul 13 '16 at 14:48
0

Based on comments to the original question, you claim you're getting this error:

Traceback (most recent call last): 
  File "./fsu.py", line 20, in <module> 
   emailElem.click() 
AttributeError: 'list' object has no attribute 'click' 

That sounds like you're calling one of the get_elements_* functions rather than get_element_* (note the plural elements).

Error messages carry a lot of information in them, and need to be taken literally. Your error message is telling you that a list doesn't have a click method, which is true. Reading between the lines it means that the object on which you're calling .click() is a list, even though you think it is an element.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank you so much @Bryan Oakley! Is there any way to select it? From the [Selenium Docs](http://selenium-python.readthedocs.io/api.html?highlight=radio#selenium.webdriver.remote.webelement.WebElement.is_selected) I gather that it must be `is_selected()` But I'm not sure how to implement that – Carolyn Jul 13 '16 at 15:08
  • @user6581117: does the following answer your question? http://stackoverflow.com/q/21322116/7432 – Bryan Oakley Jul 13 '16 at 15:38
  • `selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"input[type='radio']"}` I tried several of the answers but none seem to work and I continue to get a similar variation of this error. – Carolyn Jul 13 '16 at 17:07
  • Also received this error `emailElem = browser.find_elements_by_css_selector("input[type='radio'][value='SSR_DUMMY_RECV1$sels$0']")[0] IndexError: list index out of range` at one point but changing the index didn't solve it either. I greatly appreciate your help though :) – Carolyn Jul 13 '16 at 17:07