1

I'm using Selenium 3.12.0 with Python 3.7.2 and Firefox 66.0.1 on Fedora 29. I'm having trouble clicking a radio button. The radio button is inside a label, and the radio and label use the same name. The page is located at https://complaints.donotcall.gov/complaint/complaintcheck.aspx.

<label for="PrerecordMessageYESRadioButton">
    <input id="PrerecordMessageYESRadioButton" type="radio" name="PrerecMsg" value="PrerecordMessageYESRadioButton" tabindex="7">
    <label for="PrerecordMessageYESRadioButton">Yes</label>
</label>

When I examine a screenshot after the page has been completed, I see the radio buttons are not clicked. The other elements on the page are completed OK.

I've tried driver.find_element_by_id("PrerecordMessageYESRadioButton"), driver.find_element_by_name("PrerecMsg") and driver.find_element_by_css_selector("input#PrerecordMessageYESRadioButton"). Once selected, I've also tried radio.click(), radio.send_keys(Keys.ENTER), and radio.send_keys(Keys.SPACE) with no joy. Finally, driver.execute_script("arguments[0].click();", radio) was not helpful, either.

How does one click the radio button coupled to a label in this case?


Radio buttons seem to cause a fair amount of trouble. Here are some related questions, but they did not help in this instance of the problem. The first reference and @yong's answer seems very relevant to this problem.


Here is the test script:

$ cat test-driver.py
#!/usr/bin/env python3

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

def main():

    opts = Options()
    opts.headless = True
    driver = webdriver.Firefox(options=opts)   

    #################################################

    print("Fetching page 1")    
    driver.get("https://complaints.donotcall.gov/complaint/complaintcheck.aspx")

    print("Clicking Continue")
    button_continue = driver.find_element_by_id("ContinueButton")
    button_continue.click()

    #################################################

    print("Fetching page 2")    
    time.sleep(2) 

    text_phone = driver.find_element_by_id("PhoneTextBox")
    for ch in "8005551212":
        text_phone.send_keys(ch)

    text_calendar = driver.find_element_by_id("DateOfCallTextBox")
    for ch in "03/30/2019":
        text_calendar.send_keys(ch)

    dropdown_hour = driver.find_element_by_id("TimeOfCallDropDownList")
    dropdown_hour.send_keys("10")

    dropdown_minute = driver.find_element_by_id("ddlMinutes")
    dropdown_minute.send_keys("30")

    # PrerecordMessageYESRadioButton
    radio_robocall = driver.find_element_by_name("PrerecMsg")
    # radio_robocall = driver.find_element_by_css_selector("input#PrerecordMessageYESRadioButton")
    radio_robocall.send_keys(Keys.ENTER)
    radio_robocall.send_keys(Keys.SPACE)
    ...

    driver.quit()


if __name__ == "__main__":
    main()

Enumerating the elements on the page by id:

ids = driver.find_elements_by_xpath('//*[@id]')
for val in ids:
    print(val.get_attribute('id'))

Returns the following:

Head1
_fed_an_ua_tag
bdyComplaint
top
changeLang
topnav
navbtn
mobileChangeLang
Form1
__EVENTTARGET
__EVENTARGUMENT
__VIEWSTATE
__VIEWSTATEGENERATOR
__EVENTVALIDATION
StepOnePanel
StepOneEntryPanel
ErrorMsg
PhoneTextBox
DateOfCallTextBox
TimeOfCallDropDownList
ddlMinutes
PrerecordMessageYESRadioButton
PrerecordMessageNORadioButton
PhoneCallRadioButton
MobileTextMessageRadioButton
ddlSubjectMatter
spnTxtSubjectMatter
txtSubjectMatter
StepOneContinueButton
hdnBlockBack
hdnPhoneChecked
hdnCompanyChecked
hdnPhoneNumber

Here is what I am seeing after fetching the screenshot.

enter image description here

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    Tried your code, `driver.find_element_by_id("PrerecordMessageYESRadioButton").click()` worked fine for me (Selenium 3.141, Python 3.7.2). – Guy Mar 31 '19 at 12:48
  • I'm curios, why do you type the text char by char to the fields? – Guy Mar 31 '19 at 12:49
  • Thanks @Guy. I am working remotely over SSH. Could the remote headless screenshot be the problem? In this case, then there may not be a problem. I use character entry to simulate typing by a user. – jww Mar 31 '19 at 12:52
  • Actually I doubt that. Do you have a way to check the "results" of the form? if the values has been updated in some DB or somewhere else? You can also try running it on regular (not headless) browser and see what happens. – Guy Mar 31 '19 at 12:57
  • Add `radio_robocall.click()` and `print(f"radio_robocall status: {str(radio_robocall.is_selected())}")`after `radio_robocall = driver.find_element_by_name("PrerecMsg")` to check if radio is selected – Sers Mar 31 '19 at 13:05
  • Thanks @Guy. I added the screenshot. It is interesting (in a morbid sort of way) it works for you but not me. I may be able to cross-check some of the results by inspecting the [data files the FTC publishes](https://www.ftc.gov/site-information/open-government/data-sets/do-not-call-data). However, the FTC publishes the new daily list on weekday mornings, so I usually have to wait one day. – jww Mar 31 '19 at 13:07
  • @jww It seems the radio buttons are missing from the page (open the page locally to see the difference). Maybe it's an old FF version? – Guy Mar 31 '19 at 13:10
  • Thanks again @Guy. When I attach a monitor and login to the computer, I can open the page in Firefox and it renders as expected. At the same time, the problem persists when testing headless. – jww Mar 31 '19 at 13:47
  • Thanks @Sers. `radio_robocall YES status: True`. I also checked `PrerecordMessageNORadioButton`, and it returned `radio_robocall NO status: False`. I'm guessing that means this is a headless GUI problem, and not a real problem. – jww Mar 31 '19 at 13:52
  • 1
    @jww it's mean radio selected. Don't pay attention to screenshot. To be sure add `print(f"radio_robocall status: {str(radio_robocall.is_selected())}")` before click also, result should be `False` – Sers Mar 31 '19 at 13:55
  • Thanks again @Sers. Adding `radio_robocall.is_selected()` before the click returned `radio_robocall YES status: False`. I think it is a bug in the testing gear. – jww Mar 31 '19 at 13:58
  • 1
    @Guy - The FTC just published their daily list. The number I added using Selenium was present in the list. So it looks the radio button is checked even though the screenshot does not show it. Thanks again for your help. – jww Apr 01 '19 at 19:22

1 Answers1

1

Please check radio status using is_selected:

radio_robocall = driver.find_element_by_name("PrerecMsg")
# is_selected should return False
print(f"radio_robocall status: {str(radio_robocall.is_selected())}")

radio_robocall.click()
# is_selected should return True
print(f"radio_robocall status: {str(radio_robocall.is_selected())}")
Sers
  • 12,047
  • 2
  • 12
  • 31
  • 1
    Thanks, `radio_robocall.is_selected` worked as expected. I was struggling with this issue for two days. Sigh... – jww Mar 31 '19 at 14:03
  • One more issue if you can lend your expertise: [Selenium chromedriver exception when enumerating elements by xpath](https://stackoverflow.com/q/55763358/608639). – jww Apr 19 '19 at 14:26