How to select checkbox using selenium web driver

dharmesh mehta :

In my web-page (or popup) there are multiple input-boxes and checkbox. Input boxes and check-boxes are in separate div tag. Here is my html code:

<div class="modal-body-large">
    <div class="col-md-12 step-forms custom-tab-content">
        <form class="form-horizontal form-sections">
            <div class="form-group">
                <label class="control-label col-sm-2">Username<span class="red">*</span></label>
                <div class="col-sm-10">
                    <input name="userId" class="form-control custom-form-control" type="text" placeholder="Username" value="">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2">Email<span class="red">*</span></label>
                <div class="col-sm-10">
                    <input name="email" class="form-control custom-form-control" type="text" placeholder="Email" value="">
                </div>
            </div>
            .....
        </form>
    </div>
    <div class="col-md-12 step-forms custom-tab-content">
        <form class="form-horizontal"><span class="help-block" style="font-size: small;"><i>Note: Optional</i></span>
            <div class="col-md-6">
                <div>
                    <div class="form-sections">
                        <ul>
                            <li>Select permissions</li>
                            <li>
                                <input type="checkbox" id="permissions1565851434728" name="permissions">
                                <label for="permissions1565851434728" class="xh-highlight">Select all</label>
                            </li>
                        </ul>
                        <div class="searchbox-container">
                            <div class="check-list">
                                <ul>
                                    <li title="">
                                        <input type="checkbox" id="371565851434728" name="permissions" value="Add/Update Network Security">
                                        <label for="371565851434728" class="">Add/Update Network Security</label>
                                    </li>
                                    <li title="">
                                        <input type="checkbox" id="111565851434728" name="permissions" value="Add/Update Permissions">
                                        <label for="111565851434728" class="">Add/Update Permissions</label>
                                    </li>
                                    .....
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-sections">
                    <ul>
                        <li>Select roles</li>
                        <li>
                            <input type="checkbox" id="roles1565851434730" name="roles">
                            <label for="roles1565851434730">Select all</label>
                        </li>
                    </ul>
                    <div class="searchbox-container">
                        <div class="check-list">
                            <ul>
                                <li title="Update User Details,Create User,Create Project">
                                    <input type="checkbox" id="role-c98974c6-a4b1-4428-9d9e-7df9f00acd351565851434730" name="roles" value="test">
                                    <label for="role-c98974c6-a4b1-4428-9d9e-7df9f00acd351565851434730">test</label>
                                </li>
                                .....
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

When i come to this page, i am successfully able to enter text in input fields. When it comes to select checkbox i have to give complete xpath to that checkbox like if i want to select checkbox Select all i am giving xpath as

"/html/body/div[@id='app']/div[@class='_loading-overlay']/main/div[@class='container-fluid search_table_container']/div[1]/div[@class='identity-management']/div[@class='row identity-list']/div/div[@class='filter-head col-md-12']/div[@class='search_right']/div[@class='modal fade in']/div[@class='modal-dialog modal-lg']/div[@class='modal-content']/div[@class='modal-body-large']/div[@class='col-md-12 step-forms custom-tab-content'][2]/form[@class='form-horizontal']/div[@class='col-md-6'][1]/div/div[@class='form-sections']/ul/li[2]/label"

My concern is there any alternate to do this?

DebanjanB :

To click() on the check box associated with the <label> as Select all as the desired element is within a Modal Dialog you have to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategy:

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text()='Select permissions']//following::li[1]//label"))).click();
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161828&siteId=1