有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从不同级别的div定义xpath

在下面的HTML中,我想将xpath定义为一个,但对象标识符位于不同的div级别

以蓝色和黄色突出显示的div屏幕截图

enter image description here

实际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">&nbsp;</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) 个答案

  1. # 1 楼答案

    使用以下XPath:

    //input[@aria-checked='true']//parent::node()//following-sibling ::span//span[contains(text(),'Inforce')]