How to click on a checkbox with label using selenium node.js?

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago


I have a html which has multiple checkbox classes each with different texts that is, each check box with a different name. This is the HTML,

<div class="col-md-12 syllabus-div-1">
    <h4 class="vertical-spacing">Coaching<i class="fa fa-graduation-cap" aria-hidden="true" style="margin-left:5px; cursor:pointer" data-topic-url="/coach-topic-detail/41/"></i>h4>
    <div class="checkbox">
        <label>
            <input type="checkbox" checked="" disabled="">Manage the Coaching Process<a href="https://pluma.myhbp.org/hmm12/content/coaching/coaching_done_right.html" target="_blank"><i class="fa fa-external-link" aria-hidden="true" style="margin-left:5px;"></i>a> label>
        <p>Skipped on 03/16/2017p>
    div>
    <div class="checkbox">
        <label>
            <input type="checkbox" checked="" disabled="">Coaching Done Right<a href="https://pluma.myhbp.org/hmm12/content/coaching/manage_the_coaching_process.html" target="_blank"><i class="fa fa-external-link" aria-hidden="true" style="margin-left:5px;"></i>a> label>
        <p>Skipped on 03/16/2017p>
    div>
    <div class="checkbox">
        <label>
            <input type="checkbox" checked="" disabled="">Listen and Question Effectively<a href="https://pluma.myhbp.org/hmm12/content/coaching/listen_and_question_effectively.html" target="_blank"><i class="fa fa-external-link" aria-hidden="true" style="margin-left:5px;"></i>a> label>
        <p>Skipped on 03/16/2017
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

Based on the HTML you provided, you're not able to check/uncheck the checkboxes by clicking them because they are disabled. Moreover, all the checkboxes are already checked.

To answer your question, though, you are able to click the checkbox based the text inside the label. You can find a list of all the labels containing the checkboxes using the css selector '.checkbox label', which should return a list of labels.

Then, you can iterate over the list of labels, checking if each label has the innerText attribute with the value you, eg. 'Manage the Coaching Process'. If it has your desired innerText value, then you can store that element in a variable and kill your loop.

Once you have the selected label element you can use it to find the input checkbox among it's children and store it. No you have the checkbox. Click it and you're done.

Code Sample

Note: This sample is only meant to illustrate the logic. It's likely incorrect in terms of syntax because I don't know what JS implementation of selenium you're using.

var labels = driver.findElements(By.cssSelector('.checkbox label'));

# assumes that only one label will fit the criteria of having the desired innerText
var myLabel = labels.filter(function(label) {
   return label.getAttribute('innerText') == 'Manage the Coaching Process';
})[0];

var myCheckbox = myLabel.findElement(By.cssSelector('input[type=\'checkbox\']'));
myCheckbox.click();

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.