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;
I declare e
and assign it to the area where the checkbox is.
isChecked = e.findElement(By.tagName("input")).getAttribute("checked").equals("true");
What is weird is that getAttribute("checked")
returns null
and therefore a NullPointerException
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?
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.
So what you would look for in Selenium Webdriver is this
isChecked = e.findElement(By.tagName("input")).Selected;
As there is no Selected in WebDriver Java API, the above code should be as follows:
isChecked = e.findElement(By.tagName("input")).isSelected();