用Python利用selenium操作点击复选框的时候,出现 Other element would receive the click错误。
要点击的复选框情况如图:
首次的时候,我用以下代码定位到复选框,并且点击
- alert_input = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '//input[@name="isMarketingEnabled" and @type="checkbox"]')))
- alert_input.click()
能定位到这个input,但是在click时报错,信息如下:
- selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input type="checkbox" name="isMarketingEnabled" class="css-jiaquy" value=""> is not clickable at point (559, 419).
- Other element would receive the click: <div class="css-lbyitw Checkbox-box">...</div>
经搜索,问题的原因是Actual element is not visible within browser dimension and covered by some overlay element.(实际元素在浏览器维度内不可见,并被某些覆盖元素覆盖。),可以看到这里的input是被一个div覆盖住了。
解决代码:
- from selenium.webdriver import ActionChains
- alert_input = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '//input[@name="isMarketingEnabled" and @type="checkbox"]')))
- actions = ActionChains(driver)
- actions.move_to_element(alert_input).click().perform()
实测实现点击选中复选框的目标。