尝试单击CheckBox时出现“元素单击被拦截”错误

时间:2019-11-11 21:20:21

标签: python selenium checkbox

加载页面后,我想单击一个复选框,但是出现以下错误消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <img class="checkboximage" src="/images/nav/ns_x.gif" alt=""> is not clickable at point (843, 7). Other element would receive the click: <a class="ns-help" onclick="nlPopupHelp('EDIT_TRAN_CUSTINVC');" tabindex="0" onkeypress="(event.keyCode == 13 || event.charCode == 32) &amp;&amp; nlPopupHelp('EDIT_TRAN_CUSTINVC');">...</a>

这就是我现在所拥有的:在此之前,我已经尝试了其他几种方法

collectionsbox = driver.find_element(By.CSS_SELECTOR,"#custbody_in_collections_fs_inp")
                driver.execute_script("arguments[0].scrollIntoView(true)", collectionsbox)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='custbody_in_collections_fs']//img[@class='checkboximage']"))).click()

collectionsbox.click() #USUALLY FAILS RIGHT HERE

savebutton = driver.find_element(By.XPATH,"//input[@id='btn_multibutton_submitter']")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='btn_multibutton_submitter']"))).click()

driver.execute_script("arguments[0].scrollIntoView(true)", savebutton)
savebutton.click()
time.sleep(2)
driver.switch_to.alert.accept()

我尝试了EC方法,等待了几秒钟,向下滚动以查看元素。我什至使用过Xpath,ID和CSS。

<input onclick="setEventCancelBubble(event); this.isvalid=(nlapiValidateField(null,'custbody_in_collections')); if (this.isvalid) {setWindowChanged(window, true);nlapiFieldChanged(null,'custbody_in_collections');;} else if ( window.loadcomplete &amp;&amp; !document.page_is_resetting ) {setFormValue(this, !this.checked);}" aria-labelledby="custbody_in_collections_fs_lbl" onchange="NLCheckboxOnChange(this); " onkeypress="NLCheckboxOnKeyPress(event);  return true;" name="custbody_in_collections" id="custbody_in_collections_fs_inp" type="checkbox" value="T" class="checkbox" style="">
<input type="hidden" name="custbody_in_collections_send" style="">
<img class="checkboximage" src="/images/nav/ns_x.gif" alt="" style="">

我所需要的只是单击复选框,然后单击顶部保存。

2 个答案:

答案 0 :(得分:1)

似乎DOM上的另一个元素无意间隐藏了您要单击的元素。您可以尝试为点击执行Javascript。通常,这可以为我解决问题。

您可以用以下内容代替collectionsbox.click() #USUALLY FAILS RIGHT HERE

driver.execute_script("arguments[0].click();", collectionsbox)

答案 1 :(得分:0)

到地址元素在(x,y)错误点不可单击。我们可以按照以下方法解决此问题

解决方案

1。动作类

WebElement element = driver.findElement(By.id("yourelement ID"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

2。元素未获得点击,因为它不在视口中

使用JavascriptExecutor在视口中获取元素:

checkBox=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"yourelement Xpath")))
ActionChains(driver).move_to_element(checkBox).click(checkBox).perform()