无法选中复选框

泰克纳斯

我正在尝试检查复选框,但出现以下错误

selenium.common.exceptions.NoSuchElementException: Message: no such 
element: Unable to locate element: {"method":"xpath","selector":"//mat- 
checkbox[@id='mat-checkbox-1']/label/div"}

我尝试使用 id、css-selector 和 xpath 进行选择,但一直出错。

reg_date_checkbox = self.browser.find_element_by_xpath("//matcheckbox[@id='mat-checkbox-1']/label/div")
reg_date_checkbox.location_once_scrolled_into_view
reg_date_checkbox.click()

复选框的 HTML 是

<div _ngcontent-fep-c23="" class="last-date my-12 ng-star-inserted" fxlayout="column" style="flex-direction: column; box-sizing: border-box; display: flex;">
<mat-checkbox _ngcontent-fep-c23="" class="mat-checkbox mat-accent ng-untouched ng-pristine ng-valid" formcontrolname="deadline_enabled" id="mat-checkbox-1">
    <label class="mat-checkbox-layout" for="mat-checkbox-1-input">
        <div class="mat-checkbox-inner-container">
            <input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" id="mat-checkbox-1-input" tabindex="0" aria-checked="false">
            <div class="mat-checkbox-ripple mat-ripple" matripple="">
                <div class="mat-ripple-element mat-checkbox-persistent-ripple"></div>
            </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>The event has a registration deadline</span>
    </label>
</mat-checkbox>
<!---->

PRERNA PAL

您可能正在尝试单击不是“复选框”类型的元素。我看到有一个带有类型复选框的输入。请尝试下面提到的代码

reg_date_checkbox = self.browser.find_element_by_xpath("//input[@id='mat-checkbox-1-input']")
reg_date_checkbox.click()

此外,如果您希望执行取消选中,您可以在执行单击之前查找输入元素的 aria-checked 属性是否为真。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章