Dominik Lemberger Dominik Lemberger - 1 year ago 101
HTML Question

XPath help needed - Selecting Checkbox

<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-header-active ui-corner-top ng-binding ng-scope ui-state-active" ng-click="updateActiveTab($index)" role="tab" ng-class="{'ui-state-active': == $index}" ng-if="isUserPermitted($index) && feature.visible" ng-repeat-start="feature in model.features">
<input class="ng-pristine ng-untouched ng-valid" type="checkbox" ng-click="$event.stopPropagation()" style="vertical-align: middle;" ng-change="validateAndUpdateModel($index)" ng-model="ui.featuresEnabled[$index]"/>
AUS - Demand Response Modes (DRM)
<!-- nasty hack to fix line break for validation error messages -->
<!-- ngIf: != $index && (feature.label|translate).length >17 -->
<!-- ngIf: == $index && (feature.label|translate).length >17 -->
<span class="glyphicon glyphicon-collapse-up ng-scope" style="float: right; vertical-align: middle; margin-right: 4px" ng-if=" == $index && (feature.label|translate).length >17"/>
<!-- end ngIf: == $index && (feature.label|translate).length >17 -->
<!-- ngIf: (feature.label|translate).length >17 -->
<br class="ng-scope" ng-if="(feature.label|translate).length >17"/>
<!-- end ngIf: (feature.label|translate).length >17 -->
<!-- ngIf: showFeatureValidation(feature) -->
<!-- ngIf: !showFeatureValidation(feature) && showDoubleUsageValidation(feature) -->
<!-- ngIf: != $index && (feature.label|translate).length <=17 -->
<!-- ngIf: == $index&& (feature.label|translate).length <= 17 -->

This is the HTML, where I need my XPath. It contains a Checkbox and the Label of the CheckBox (AUS - Demand Response Modes (DRM)), in the whole HTML there are multiple Tags, therefore I need a way to ensure the checkbox I select contains the right Label - Like in this example the Demand...

What i tried so far is:

//*[@role='tab' and contains(text(),'Demand')]//*[@type='checkbox']

And some other versions that mostly look the same, but i cant get it to work properly it either shows me all the checkboxes but not linked with the Label within the h3 Tag, or none at all.

Answer Source

You can use the XPath:

//*[@role='tab']//*[@type='checkbox' and contains(./following-sibling::node()[1]/self::text(), 'Demand')]

and it will return the input checkbox (that is a descendant of an element with the role="tab" attribute) that is immediately followed by a text node that contains the word Demand.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download