无法验证选中的复选框,Selenium Webdriver Java

时间:2019-10-30 10:54:56

标签: java selenium-webdriver

在测试中,我要验证选中的复选框。下面是我的代码

if (CheckboxForFirstCitizenship.isSelected()) {
        System.out.println("Checkbox selected");
    } else {
        System.out.println("Checkbox not selected");
    }
    if (CheckboxForSecondCitizenship.isSelected()) {
        System.out.println("Checkbox selected");
    } else {
        System.out.println("Checkbox not selected");
    }

我在PageObjects类中拥有的Web元素

@FindBy(xpath = "/html[1]/body[1]/ufe-root[1]/div[1]/div[1]/div[1]/ng-component[1]/div[1]/ufe-klient[1]/app-dashboard[1]/main[1]/div[1]/div[2]/app-individual-client-edit-container[1]/app-process-container[1]/app-local-loader[1]/div[1]/div[3]/app-individual-client-edit-client-data[1]/app-form-container[1]/p-accordion[1]/div[1]/p-accordiontab[1]/div[2]/div[1]/div[2]/div[1]/app-form-data-set-container[1]/div[1]/div[1]/app-individual-client-edit-personal-data-form[1]/app-data-container[1]/div[1]/div[2]/app-container-state[1]/div[1]/form[1]/div[1]/div[8]/div[1]/div[1]/app-individual-client-edit-citizenship-form[1]/div[2]/p-checkbox[1]/div[1]/div[2]")
@CacheLookup
public WebElement CheckboxForFirstCitizenship;

@FindBy(xpath = "/html[1]/body[1]/ufe-root[1]/div[1]/div[1]/div[1]/ng-component[1]/div[1]/ufe-klient[1]/app-dashboard[1]/main[1]/div[1]/div[2]/app-individual-client-edit-container[1]/app-process-container[1]/app-local-loader[1]/div[1]/div[3]/app-individual-client-edit-client-data[1]/app-form-container[1]/p-accordion[1]/div[1]/p-accordiontab[1]/div[2]/div[1]/div[2]/div[1]/app-form-data-set-container[1]/div[1]/div[1]/app-individual-client-edit-personal-data-form[1]/app-data-container[1]/div[1]/div[2]/app-container-state[1]/div[1]/form[1]/div[1]/div[8]/div[1]/div[2]/app-individual-client-edit-citizenship-form[1]/div[2]/p-checkbox[1]/label[1]")
@CacheLookup
public WebElement CheckboxForSecondCitizenship;

enter image description here

在测试结果中,尽管选择了firstCheckox,但我还是有两条打印线“未选中复选框”。我做错了什么?有人可以帮忙吗?

<p-checkbox _ngcontent-c26="" binary="true" class="control__hint ng-untouched ng-pristine ng-valid ng-star-inserted" label="Główne" id="main-citizenship-checkbox-0" style="" xpath="1">
    <div class="ui-chkbox ui-widget">
        <div class="ui-helper-hidden-accessible">
            <input type="checkbox" name="undefined" value="undefined">
        </div>
        <div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default ui-state-active"><span class="ui-chkbox-icon ui-clickable pi pi-check"></span></div>
    </div>
    <!---->
    <label class="ui-chkbox-label ui-label-active ng-star-inserted">Główne</label>
</p-checkbox>

1 个答案:

答案 0 :(得分:1)

要使用...方法,必须处理isSelected元素。
将CSS选择器input type="checkbox"用于第一个复选框。
可能第二个是#main-citizenship-checkbox-0 input

#main-citizenship-checkbox-1 input