Selenium Python点击Yes单选框遭遇ElementClickInterceptedException错误求助
问题
页面存在两个单选框“Yes”和“No”,默认选中“No”。尝试点击“Yes”单选框时持续触发以下错误:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="radio" id="si_notificar" name="notificar" value="1" class="custom-control-input notificar"> is not clickable at point (361, 308). Other element would receive the click: <label class="custom-control-label text-sm" for="si_notificar">...</label>
已尝试通过XPath、ID定位,也用过WebDriverWait(甚至改用延长时长的time.sleep排除等待问题),但错误依旧。
相关元素代码:
- YES单选框:
<input type="radio" id="si_notificar" name="notificar" value="1" class="custom-control-input notificar">
- NO单选框:
<input type="radio" id="no_notificar" name="notificar" value="0" class="custom-control-input notificar" checked="">
当前使用的代码:
time.sleep(15) driver.find_element(By.ID,"si_notificar").click()
解决方案
这个错误是因为单选框的点击区域被关联的<label>元素覆盖了,实际页面中用户也是通过点击label来选择单选框的,可尝试以下几种解决方法:
点击对应的label元素
直接定位并点击与单选框绑定的label(通过for属性匹配单选框ID):time.sleep(15) driver.find_element(By.CSS_SELECTOR, 'label[for="si_notificar"]').click()用JavaScript强制触发点击
绕过元素遮挡问题,直接通过JS执行单选框的点击事件:time.sleep(15) radio_element = driver.find_element(By.ID, "si_notificar") driver.execute_script("arguments[0].click();", radio_element)通过ActionChains模拟鼠标点击
用动作链模拟鼠标移动到单选框位置后点击:from selenium.webdriver.common.action_chains import ActionChains time.sleep(15) radio_element = driver.find_element(By.ID, "si_notificar") ActionChains(driver).move_to_element(radio_element).click().perform()
内容的提问来源于stack exchange,提问作者emigoti
相关产品推荐
相关产品推荐

