jww
jww

Reputation: 102376

Click radio button coupled to a label with same name using Firefox WebDriver

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

Upvotes: 1

Views: 200

Answers (1)

Sers
Sers

Reputation: 12255

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())}")

Upvotes: 1

Related Questions