java如何从不同级别的div定义xpath
在下面的HTML
中,我想将xpath
定义为一个,但对象标识符位于不同的div级别
以蓝色和黄色突出显示的div屏幕截图
实际HTML如下:
<div class="option mli-body-1 selected ng-star-inserted" tabindex="0" ng-reflect-klass="option mli-body-1" ng-reflect-ng-class="[object Object]">
<mat-checkbox class="mat-checkbox mat-accent mat-checkbox-checked" ng-reflect-checked="true" id="mat-checkbox-97">
<label class="mat-checkbox-layout" for="mat-checkbox-97-input">
<div class="mat-checkbox-inner-container">
<input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" id="mat-checkbox-97-input" tabindex="0" aria-label="" aria-checked="true">
<div class="mat-checkbox-ripple mat-ripple" matripple="" ng-reflect-centered="true" ng-reflect-radius="25" ng-reflect-animation="[object Object]" ng-reflect-disabled="false" ng-reflect-trigger="[object HTMLLabelElement]"></div>
<div class="mat-checkbox-frame"></div>
<div class="mat-checkbox-background">
<svg xml:space="preserve" class="mat-checkbox-checkmark" focusable="false" version="1.1" viewBox="0 0 24 24">
<path class="mat-checkbox-checkmark-path" d="M4.1,12.7 9,17.6 20.3,6.3" fill="none" stroke="white"></path>
</svg>
<div class="mat-checkbox-mixedmark"></div>
</div>
</div>
<span class="mat-checkbox-label">
<span style="display:none"> </span>
<mli-selected-text ng-reflect-text="Inforce (3)" ng-reflect-highlighted="">
<span>Inforce (3)</span>
</mli-selected-text>
</span>
</label>
</mat-checkbox>
</div>
“aria checked”必须是TRUE
,span内的文本必须是'Inforce'
我使用的是这段代码,但问题是,即使文本不是信息,它也总能找到对象
//input[@aria-checked='true']/following::span[contains(text(),'Inforce')]
# 1 楼答案
使用以下XPath: