java - 如何使用java清除Selenium中的下拉菜单

标签 java selenium

我想清除选择类型的下拉菜单中的数据,但我无法使用 select.clear(),因为它返回错误“元素必须是用户可编辑的才能清除它”,这完全有道理。你们中有人知道使用什么命令来清除相应的字段吗?

最佳答案

如果您的元素有 <select>标签,可以使用Select类进行交互。

// first, initialize the WebElement (using the appropriate selector type, this is just an example)
WebElement element = driver.findElement(By.id("elementId"));

// next, pass it to the Select object
Select selectElement = new Select(element);

// then, select an option from the element
selectElement.selectByVisibleText("Option Text");

我们还可以通过索引或值进行选择 ( documentation )

// finally, to clear your selection, try:
selectElement.deselectAll();

关于java - 如何使用java清除Selenium中的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39230339/

相关文章:

java - 正则表达式 - 测试 9 个相邻的相同数字

java - Android 无法正确处理 Executor 中的 AssertionError?

python - 陈旧元素引用 : element is not attached to the page document with the chrome web driver

java - 动态表数据检索 - selenium webdriver

firefox - 关闭 Firefox 13 中的内置 RSS 阅读器

java lwjgl 设置鼠标位置而不更改 dx 或 dy

java - 使用 Spring JPA 未将 Rest api 响应数据保存在数据库中

java - 正则表达式查询(运行时可定制)

html - 如何找到框架集中元素的 XPath?

java - 无法测试我的网页中的错误消息