判断 Selenium for Java 是否选了复选框?
问题说明
我在 Java 中使用 Selenium 来测试 Web 应用程序中复选框的检查.代码如下:
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();
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfhijh
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22