title: How do you tell if a checkbox is selected in Selenium for Java?
body: <p>I am using <a href="https://www.selenium.dev/" rel="noreferrer">Selenium</a> in Java to test the checking of a checkbox in a webapp. Here's the code:</p>
  <pre><code>private boolean isChecked;
  private WebElement e;
  </code></pre>
  <p>I declare <code>e</code> and assign it to the area where the checkbox is.</p>
  <pre><code>isChecked = e.findElement(By.tagName(&quot;input&quot;)).getAttribute(&quot;checked&quot;).equals(&quot;true&quot;);
  </code></pre>
  <p>What is weird is that <code>getAttribute(&quot;checked&quot;)</code> returns <code>null</code> and therefore a <code>NullPointerException</code></p>
  <p>In the HTML for the checkbox, there is no <code>checked</code> attribute displayed. However, isn't it the case that all <code>input</code> elements have a <code>checked = &quot;true&quot;</code> so this code should work?</p>