How do you tell if a checkbox is selected in Selenium for Java?(如何判断 Selenium for Java 中是否选中了一个复选框?)
问题描述
我在 Java 中使用 Selenium 来测试 webapp 中复选框的检查.代码如下:
I am using Selenium in Java to test the checking of a checkbox in a webapp. Here's the code:
private boolean isChecked;
private WebElement e;
我声明 e
并将其分配给复选框所在的区域.
I declare e
and assign it to the area where the checkbox is.
isChecked = e.findElement(By.tagName("input")).getAttribute("checked").equals("true");
奇怪的是 getAttribute("checked")
返回 null
并因此返回 NullPointerException
What is weird is that getAttribute("checked")
returns null
and therefore a NullPointerException
在复选框的 HTML 中,没有显示 checked
属性.但是,不是所有 input
元素都有一个 checked = true"
所以这段代码应该可以工作吗?
In the HTML for the checkbox, there is no checked
attribute displayed. However, isn't it the case that all input
elements have a checked = "true"
so this code should work?
推荐答案
如果您使用的是 Webdriver,那么您要查找的项目是 Selected.
If you are using Webdriver then the item you are looking for is Selected.
通常在复选框的渲染中,除非指定,否则实际上不会应用选中的属性.
Often times in the render of the checkbox doesn't actually apply the attribute checked unless specified.
所以你会在 Selenium Webdriver 中寻找的是这个
So what you would look for in Selenium Webdriver is this
isChecked = e.findElement(By.tagName("input")).Selected;
由于WebDriver Java API中没有Selected,上面的代码应该如下:
As there is no Selected in WebDriver Java API, the above code should be as follows:
isChecked = e.findElement(By.tagName("input")).isSelected();
这篇关于如何判断 Selenium for Java 中是否选中了一个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何判断 Selenium for Java 中是否选中了一个复选框?


- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01