Python 无法选中复选框
我试图选中复选框,但我得到以下错误Python 无法选中复选框,python,selenium,Python,Selenium,我试图选中复选框,但我得到以下错误 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选择器和xpath进行选择,但不断出现错误 reg_date_chec
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选择器和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"> </span>The event has a registration deadline</span>
</label>
</mat-checkbox>
<!---->
该活动有一个注册截止日期
您正在使用这个xpath
//matcheckbox[@id='mat-checkbox-1']/label/div
,它应该是//mat-checkbox[@id='mat-checkbox-1']/label/div
,中间有一个-
,您缺少它
您仍然可以尝试使用此css选择器:
div.mat-checkbox-inner-container input[class^='mat-checkbox-input'][id^='mat-checkbox']
该复选框可能位于iframe中,如果是这样,请先切换到frame,然后尝试单击该复选框。您可以尝试以下操作:
element = self.browser.find_element_by_css("input#mat-checkbox-1-input")
element.click()
您可能正在尝试单击非“checkbox”类型的元素。我看到有一个类型为checkbox的输入。请尝试下面提到的代码
reg_date_checkbox = self.browser.find_element_by_xpath("//input[@id='mat-checkbox-1-input']")
reg_date_checkbox.click()
此外,如果希望执行取消选中,可以在执行单击之前查找输入元素的aria checked属性是否为true。我收到以下错误selenium.common.exceptions.element ClickInterceptedException:Message:element click intercepted:element在点(421,64)处不可单击。其他元素将收到单击:…当您手动执行此操作时,是否向下滚动或使用任何类型的操作与元素交互。好的,您可以尝试使用actions类吗?