You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

下拉框选中后遮挡后续操作,ElementClickInterceptedException问题求助

问题描述

我在用代码模拟用户输入,网站包含多个带选项的下拉框,需要选择其中一个选项。登录及进入表单填写环节均正常,但选中第一个下拉框选项后,操作下一个下拉框时程序停止,且第一个下拉框出现自遮挡现象。

代码实现
// 选择下拉框第一个选项的函数
def selectFirstOptionFromDropdown(Object dropdownObject) {
    WebUI.click(dropdownObject)
    WebDriver driver = DriverFactory.getWebDriver()
    WebDriverWait wait = new WebDriverWait(driver, 60)
    List<WebElement> options = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("/html/body/div[2]/form/div/div/div[1]/div[4]/div[2]/div/ul/li")))
    //WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[2]/form/div/div/div[1]/div[4]/div[2]/div")))
    //element.click()
    options.get(1).click()
}
// 提交Annex A表单的函数
def submitAnnexA() {
    // 从下拉列表中选择第一个选项
    selectFirstOptionFromDropdown(findTestObject('Object Repository/annexA_han/Page_Annex A/div_Please SelectPlease SelectOutpatient De_b7f71f'))
    selectFirstOptionFromDropdown(findTestObject('Object Repository/annexA_han/Page_Annex A/span_Please Select'))
    selectFirstOptionFromDropdown(findTestObject('Object Repository/annexA_han/Page_Annex A/div_Please SelectPlease SelectAnaesthesiolo_d69f76'))
    selectFirstOptionFromDropdown(findTestObject('Object Repository/annexA_han/Page_Annex A/div_Please SelectPlease SelectAE (Observati_e9e2b1'))
}
实际结果

实际效果

报错信息
org.openqa.selenium.ElementClickInterceptedException: Element <li class="option selected"> is not clickable at point (900,646) because another element <div class="nice-select lhServiceClass"> obscures it
元素检查截图

元素检查

解决方案

核心问题

你用的是绝对固定XPath,只对应第一个下拉框的选项,后续下拉框的选项路径完全不同;而且第一个下拉框选择后没有自动收起,遮挡了后续操作。

修复步骤

(1)改用相对路径定位选项

基于当前点击的下拉框元素,相对定位它的选项列表,避免固定路径失效:

def selectFirstOptionFromDropdown(Object dropdownObject) {
    WebUI.click(dropdownObject)
    WebDriver driver = DriverFactory.getWebDriver()
    WebDriverWait wait = new WebDriverWait(driver, 60)
    
    // 获取当前操作的下拉框WebElement
    WebElement dropdown = WebUI.findWebElement(dropdownObject, 60)
    // 相对定位该下拉框下的所有选项
    List<WebElement> options = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("./div/ul/li"), dropdown))
    
    options.get(1).click()
    // 选择后手动收起下拉框,避免遮挡
    WebUI.click(dropdownObject)
}

(2)用JavaScript点击绕过遮挡

如果元素遮挡问题依然存在,直接用JS执行点击,不需要依赖页面坐标:

def selectFirstOptionFromDropdown(Object dropdownObject) {
    WebUI.click(dropdownObject)
    WebDriver driver = DriverFactory.getWebDriver()
    WebDriverWait wait = new WebDriverWait(driver, 60)
    
    WebElement dropdown = WebUI.findWebElement(dropdownObject, 60)
    List<WebElement> options = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("./div/ul/li"), dropdown))
    
    // 用JS点击选项
    WebUI.executeJavaScript("arguments[0].click();", [options.get(1)])
    // 用JS收起下拉框
    WebUI.executeJavaScript("arguments[0].click();", [dropdown])
}

(3)验证下拉框状态

如果下拉框展开状态不稳定,可以先等待它展开再操作:

def selectFirstOptionFromDropdown(Object dropdownObject) {
    WebUI.click(dropdownObject)
    WebDriver driver = DriverFactory.getWebDriver()
    WebDriverWait wait = new WebDriverWait(driver, 60)
    
    WebElement dropdown = WebUI.findWebElement(dropdownObject, 60)
    // 等待下拉框处于展开状态
    wait.until(ExpectedConditions.attributeToBe(dropdown, "aria-expanded", "true"))
    List<WebElement> options = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("./div/ul/li"), dropdown))
    
    options.get(1).click()
    WebUI.click(dropdownObject)
}

内容的提问来源于stack exchange,提问作者maykr03

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 20:00:12