user2987322
user2987322

Reputation: 177

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

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/2017</p>
    </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/2017</p>
    </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</p>
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" checked="" disabled="">Give Effective Feedback<a href="https://pluma.myhbp.org/hmm12/content/coaching/give_effective_feedback_coaching.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</p>
    </div>
</div>

Using selenium (node.js) can I at a given point click on a checkbox by finding the element by name. For example, can i click on a check box by name Manage the Coaching Process, I have tried to find by LinkText. Is there anyway I can click on the checkbox by grabbing the text in the <label> tag. Any help will be greatly Appreciated. TIA

Upvotes: 1

Views: 3810

Answers (1)

Onel Harrison
Onel Harrison

Reputation: 1334

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();

Upvotes: 1

Related Questions