Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (29.3k points)

I am using Selenium in Java to test the checking of a checkbox in a web-app. 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?

1 Answer

0 votes
by (50.2k points)

If you are using WebDriver then the item you are looking for is selected.

Sometimes check-box doesn’t apply the attribute unless specified, so you would look for in selenium webDriver is 

 isChecked = e.findElement(By.tagName("input")).Selected;

If there is no Selected in WebDriver Java API, then the code will be like this

isChecked = e.findElement(By.tagName("input")).isSelected();

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training and certification!

Related questions

Browse Categories