1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Selenium: Clicking on a radio button with dynamic value

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 12:02.

  1. Stack

    Stack Membro Participativo

    I'm working with a webpage that has a list of 20+ radio buttons. Part of my python script is supposed to click on a specific radio button based on its associated label text ("Certification Exam"), but I'm facing some challenges. The structure of the radio button I'm trying to click typically looks like this:

    html

    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="27162" checked="">
    Certification Exam
    </label>
    </li>


    The "value" attribute changes depending on a user's location input (States or provinces). For example, 27162 could be for Arizona vs 28156 for Arkansas.

    The only code that has consistently worked for clicking the radio button associated with "Certification Exam" is:

    try:
    certification_exam_radio = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'input[type="radio"][value="27162"]'))
    )
    driver.execute_script("arguments[0].scrollIntoView(true);", certification_exam_radio)
    driver.execute_script("arguments[0].click();", certification_exam_radio)
    except Exception as e:
    print(f"Error while clicking on the 'Certification Exam' radio button: {e}")


    However, this only works for one state since the value is hardcoded. I've tried several methods to click the radio button dynamically regardless of its value:

    Attempt 1:

    def click_certification_exam_radio(driver, exam_text="Certification Exam"):
    try:
    certification_exam_radio = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, f"//label[contains(text(), '{exam_text}')]/input[@type='radio']"))
    )
    driver.execute_script("arguments[0].scrollIntoView(true);", certification_exam_radio)
    driver.execute_script("arguments[0].click();", certification_exam_radio)
    except Exception as e:
    print(f"Error while clicking on the 'Certification Exam' radio button: {e}")

    # Usage:
    click_certification_exam_radio(driver)


    Attempt 2:

    try:
    print("Attempting to locate the 'Certification Exam' radio button...")
    certification_exam_radio = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//label[contains(text(), "Certification Exam")]/input[@type="radio"]'))
    )

    if certification_exam_radio:
    print("'Certification Exam' radio button located successfully!")
    else:
    print("'Certification Exam' radio button not found!")
    return # Exit the function or loop if the radio button is not found


    Attempt 3:

    wait = WebDriverWait(driver, 10) # wait for up to 10 seconds
    radio_buttons = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@id="basics"]//input[@type="radio"]')))

    # Select the last radio button
    last_radio_button = radio_buttons[-1]

    # Scroll to last radio button
    driver.execute_script("arguments[0].scrollIntoView();", last_radio_button)

    # JavaScript click
    driver.execute_script("arguments[0].click();", last_radio_button)


    Attempt 4:

    # Wait for the radio button to be present
    wait = WebDriverWait(driver, 10) # wait for up to 10 seconds
    radio_button = wait.until(EC.presence_of_element_located((By.XPATH, '//label[normalize-space(text())="Certification Exam"]/input[@type="radio"]')))

    # Retrieve the value attribute from the radio button
    value_attribute = radio_button.get_attribute('value')
    print(f"The value attribute of the 'Certification Exam' radio button is: {value_attribute}")

    radio_button.click()


    Attempt 5:

    try:
    certification_exam_label = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//label[contains(text(), "Certification Exam")]'))
    )

    time.sleep(1)

    # JavaScript Click
    driver.execute_script("arguments[0].click();", certification_exam_label)

    except Exception as inner_e:
    print(f"Error while clicking on the 'Certification Exam' label: {inner_e}")

    print(f"Error while clicking on the 'Certification Exam' radio button: {e}")


    Attempt 6 (Edit: Added after solution suggested):

    # Assuming you have a variable `country_option` that stores the country choice
    if country_option == "Canada":
    # Try to click on the "Transport Canada Boating Safety Test" radio button
    try:
    transport_canada_radio = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'input[type="radio"][value="114169"]'))
    )
    # Scroll to the element
    driver.execute_script("arguments[0].scrollIntoView(true);", transport_canada_radio)
    # Click using JavaScript
    driver.execute_script("arguments[0].click();", transport_canada_radio)
    except Exception as e:
    print(f"Error while clicking on the 'Transport Canada Boating Safety Test' radio button: {e}")
    else:
    selector = "//label[contains(., 'Certification Exam')]//input"
    certification_exam_radio = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(("xpath", selector))
    )
    certification_exam_radio.click()


    I'm looking for guidance on the best way to click on the radio button based on its label text, regardless of its value. Any suggestions or improvements would be great.

    Note when manually clicking the radio button the label text can also be clicked and it will click the radio button. Furthermore, the Certification Exam radio button is typically the last radio button

    Page HTML (Edit: added for more context):

    <div id="basics">
    <div class="basics-form">

    <div class="basics-group panel panel-default">
    <div class="panel-heading">
    <h3 class="panel-title">Student Progress</h3>
    </div>

    <div class="panel-body">

    <ul class="list-group">
    <li class="list-group-item">
    <a href="#segment-26755" data-bs-toggle="collapse">Unit 1: Before Getting Underway</a>
    <ul class="list-group collapse show" id="segment-26755">
    <li class="list-group-item">
    <a href="#segment-26756" data-bs-toggle="collapse">Topic 1: The Many Parts of a Boat</a>
    <ul class="list-group collapse show" id="segment-26756">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26757" checked="">
    Parts of a Boat From a Side View
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26758">
    Parts of a Boat From a Front View
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26759">
    Activity: Can You Label the Parts of a Boat?
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26760" data-bs-toggle="collapse">Topic 2: Types of Boat Hulls</a>
    <ul class="list-group collapse " id="segment-26760">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26761">
    Displacement Hulls
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26762">
    Planing Hulls
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26763">
    How Planing Hulls Operate
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26764">
    Descriptions of Hull Shapes
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26765" data-bs-toggle="collapse">Topic 3: Boat Length</a>
    <ul class="list-group collapse " id="segment-26765">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26766">
    Measuring Length Overall
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26767">
    Length Classes
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26768" data-bs-toggle="collapse">Topic 4: Types of Engines and Drives</a>
    <ul class="list-group collapse " id="segment-26768">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26769">
    Outboard Engines
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26770">
    Inboard Engines
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26771">
    Stern Drives
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26772">
    Jet Drives
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26773" data-bs-toggle="collapse">Topic 5: Personal Watercraft</a>
    <ul class="list-group collapse " id="segment-26773">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26774">
    What Is a PWC?
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26776">
    Parts of a PWC From a Front View
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26777">
    Parts of a PWC From a Side View and Operator's View
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26778">
    Activity: Can You Label the Parts of a PWC?
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26779" data-bs-toggle="collapse">Topic 6: Sailboats</a>
    <ul class="list-group collapse " id="segment-26779">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26780">
    Parts of a Sailboat
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26781" data-bs-toggle="collapse">Topic 7: Your Boat's Capacity</a>
    <ul class="list-group collapse " id="segment-26781">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26782">
    The Capacity Plate
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26783">
    Example of a Capacity Plate
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26784">
    Calculating Your Boat's Capacity
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26785" data-bs-toggle="collapse">Topic 8: Float Plans</a>
    <ul class="list-group collapse " id="segment-26785">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26786">
    File a Float Plan
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26787">
    Float Plan for Short Outings
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26788">
    Float Plan for Extended Outings
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26789" data-bs-toggle="collapse">Topic 9: Fuel Your Boat...Safely</a>
    <ul class="list-group collapse " id="segment-26789">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26790">
    Before Fueling Your Boat
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26791">
    While Fueling Your Boat
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26792">
    After Fueling Your Boat
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26793">
    Video: Fueling Your Boat
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26794">
    Fueling a PWC
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26795">
    Prevent Running Out of Fuel
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26796" data-bs-toggle="collapse">Topic 10: Trailering Your Boat</a>
    <ul class="list-group collapse " id="segment-26796">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26797">
    Choosing the Right Trailer and Towing Vehicle
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26798">
    The Towing Hitch
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26799">
    Trailer Safety Chains
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26800">
    Before Leaving Home With Your Boat and Trailer
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26801">
    On the Road With a Trailer
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26802">
    Launching Your Boat From a Trailer
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26803">
    Backing the Trailer Into the Water During Launch
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26804">
    Retrieving Your Boat Onto a Trailer
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26805">
    Do Not Power Load Your Boat
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26806">
    Courtesy on the Boat Ramp
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26807">
    Video: Trailering &amp; Launching
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26808" data-bs-toggle="collapse">Topic 11: Tying Nautical Knots</a>
    <ul class="list-group collapse " id="segment-26808">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26809">
    Types of Nautical Knots
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26810">
    Animation: Learn to Tie Nautical Knots
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26811" data-bs-toggle="collapse">Topic 12: Taking Care of Your Boat and Engine</a>
    <ul class="list-group collapse " id="segment-26811">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26812">
    Boat Maintenance
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26813">
    Engine Maintenance
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <a href="#segment-26814" data-bs-toggle="collapse">Topic 13: Summary</a>
    <ul class="list-group collapse " id="segment-26814">
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26815">
    What You've Learned
    </label>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26816">
    Video: Unit 1 Review
    </label>

    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="26817">
    Unit 1 Quiz
    </label>


    </li>
    </ul>

    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="27161">
    Practice Exam
    </label>


    </li>
    <li class="list-group-item">
    <label>
    <input type="radio" name="student[progress]" value="27162">
    Certification Exam
    </label>


    </li>
    </ul>

    </div>

    </div>
    </div>

    <div class="btn-group actions" role="group" aria-label="Actions">
    <button type="submit" class="btn btn-success">Save Changes</button>
    </div>
    </div>


    [​IMG]

    Edit: soluton that has worked:

    selector = "//input[@type='radio']"

    print("Attempting to locate all radio buttons on the page...")

    try:
    radio_buttons = WebDriverWait(driver, 10).until(
    EC.presence_of_all_elements_located((By.XPATH, selector))
    )

    print(f"Found {len(radio_buttons)} radio buttons!")

    for idx, radio in enumerate(radio_buttons, start=1):
    # Navigate to the parent label and get its text
    label = driver.execute_script("return arguments[0].parentNode.textContent;", radio).strip()
    value = radio.get_attribute("value")

    print(f"Radio Button {idx}:")
    print(f"\tValue = {value}")
    print(f"\tLabel = {label}")

    if "Exam" in label and "Practice" not in label:
    print("Found the target radio button. Attempting to click using JavaScript...")

    driver.execute_script("arguments[0].click();", radio)

    break # Exit loop once target radio is clicked

    # Code to handle the "Save Changes" button
    save_changes_button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'button.btn.btn-success'))
    )

    # Scroll
    driver.execute_script("arguments[0].scrollIntoView(true);", save_changes_button)
    time.sleep(1) # Give it a moment after scrolling

    # JavaScript
    driver.execute_script("arguments[0].click();", save_changes_button)
    except Exception as e:

    Continue reading...

Compartilhe esta Página