Java+Selenium实现点击按钮后等待其禁用且文本为指定值
解决方案:点击按钮后等待禁用状态与指定文本
提问(中文翻译)
我目前维护一个由其他开发者编写的Java/Selenium项目,该项目通过遍历Excel表格中的元素及其属性,根据条件执行点击、鼠标悬停等操作。其中有一个按钮元素,点击后会变为禁用状态,执行过程中显示不同文本;待网页数据加载完成后,按钮文本恢复为原内容("Apply Filter")但仍保持禁用状态。我已有根据webElementType执行对应操作的代码(如下所示),现在需要实现点击该按钮后,等待其处于禁用状态且文本为"Apply Filter"的逻辑,请问如何用Java和Selenium实现?
public void performAction() throws Throwable { List<Locator> allLocators = this.eachWebElement.getAllPossibleLocators(); String webElementType = this.eachWebElement.getElementType(); String logText = ""; try { WebElement element = getSuitableWebElement(allLocators); List<WebElement> allElements = getSuitableWebElements(allLocators); if (webElementType.equalsIgnoreCase("textBox")) { logText = "to enter " + this.eachWebElement.getInputValue() + " in " + this.eachWebElement.getElementName() + " "; enterText(element, this.eachWebElement.getInputValue().trim()); ExtentCustom.eachTest.pass(MarkupHelper.createLabel(logText, ExtentColor.GREEN)); } else if ("radiobuttoncheckboxbuttonlink".contains(webElementType.toLowerCase())) { logText = "to click on " + this.eachWebElement.getElementName() + " "; click(element); ExtentCustom.eachTest.pass(MarkupHelper.createLabel(logText, ExtentColor.GREEN)); } else if (webElementType.equalsIgnoreCase("movemouse")) { logText = " mouse is moved to " + this.eachWebElement.getElementName(); moveMouse(element); ExtentCustom.eachTest.pass(MarkupHelper.createLabel(logText, ExtentColor.GREEN)); } else if (webElementType.equalsIgnoreCase("fileupload")) { logText = " file is uploaded to " + this.eachWebElement.getElementName(); uploadFile(element, this.eachWebElement.getInputValue()); ExtentCustom.eachTest.pass(MarkupHelper.createLabel(logText, ExtentColor.GREEN)); } else { boolean selectedStatus = selectFromDropdown(allElements, this.eachWebElement.getInputValue().trim()); Assert.assertTrue(selectedStatus, this.eachWebElement.getInputValue().trim() + " not able to select, might be element value to select is wrong"); logText = "value " + eachWebElement.getInputValue().trim() + " got selected from the dropdown : " + this.eachWebElement.getElementName(); ExtentCustom.eachTest.pass(MarkupHelper.createLabel(logText, ExtentColor.GREEN)); } } catch (Throwable t) { throw t; } finally { // ExtentCustom.logResult(reportStatusLogger, logText); } }
实现步骤
你需要在按钮点击的代码分支中,添加基于WebDriverWait的复合等待逻辑,同时校验按钮的禁用状态和文本内容。具体修改如下:
1. 导入必要的包
确保代码中引入Selenium等待相关的类:
import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import org.openqa.selenium.TimeoutException;
2. 修改按钮点击分支的代码
在原代码的按钮/链接点击分支中,添加等待逻辑,通过元素名称判断是否是目标按钮(也可以用Excel中配置的其他标识,比如元素ID):
} else if ("radiobuttoncheckboxbuttonlink".contains(webElementType.toLowerCase())) { logText = "to click on " + this.eachWebElement.getElementName() + " "; click(element); ExtentCustom.eachTest.pass(MarkupHelper.createLabel(logText, ExtentColor.GREEN)); // 针对"Apply Filter"按钮添加等待逻辑 if ("Apply Filter".equalsIgnoreCase(this.eachWebElement.getElementName())) { try { // 初始化等待器,设置15秒超时(可根据页面速度调整) WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(15)); // 等待按钮同时满足:禁用状态 + 文本为"Apply Filter" wait.until(driver -> { // 每次检查都重新获取元素,避免元素失效(StaleElementReferenceException) WebElement refreshedBtn = getSuitableWebElement(allLocators); return !refreshedBtn.isEnabled() && "Apply Filter".equals(refreshedBtn.getText().trim()); }); logText = "等待" + this.eachWebElement.getElementName() + "进入禁用且文本为'Apply Filter'的状态完成"; ExtentCustom.eachTest.pass(MarkupHelper.createLabel(logText, ExtentColor.GREEN)); } catch (TimeoutException e) { logText = "等待" + this.eachWebElement.getElementName() + "达到目标状态超时"; ExtentCustom.eachTest.fail(MarkupHelper.createLabel(logText, ExtentColor.RED)); throw new RuntimeException(logText, e); } } }
3. 关键细节说明
- 重新获取元素:点击后按钮可能因页面重新渲染导致原WebElement对象失效,所以每次检查都通过
getSuitableWebElement重新定位,避免抛出StaleElementReferenceException。 - 复合条件校验:通过Lambda表达式同时判断
!isEnabled()(禁用)和文本匹配,确保两个条件同时满足才继续执行。 - 超时处理:添加超时异常捕获,记录失败日志并抛出异常,符合现有代码的错误处理逻辑。
- 标识目标按钮:这里用
elementName判断是否是目标按钮,如果Excel中有更精准的标识(比如元素的value属性或id),可以替换成对应的判断逻辑。
内容的提问来源于stack exchange,提问作者wheelerlc64
相关产品推荐
相关产品推荐

