user9347168
user9347168

Reputation:

Selenium (with Java): Cannot verify that checkbox with ID is displayed

I'm having problems with Selenium not being able to verify that a visible an interactable checkbox element is indeed displayed. The problem is only with the checkbox; other elements on the page are verified fine.

The markup (snipped):

<div>
  <input type="checkbox" id="innskuttegenkapital">
    <div class="hb-label" data-e2e-selector="innskuttegenkapitalhb-checkbox-label">
      <label for="innskuttegenkapital" id="innskuttegenkapital-label">Innskutt egenkapital</label>
    </div>
  </div>
</div>  

I'm using PageFactory to create the web elements, and assertThat to verify visible or not:

Java code from the Page Object:

@FindBy(css = "[data-e2e-selector=innskuttegenkapitalhb-checkbox-label]")
WebElement innskuttegenkapitallabel;
@FindBy(css = "#innskuttegenkapital")
WebElement innskuttegenkapitalcheckbox;

Java code from the test step:

assertThat(page.innskuttegenkapitallabel.isDisplayed()).isTrue();
assertThat(page.innskuttegenkapitalcheckbox.isDisplayed()).isTrue();

The first assert (for the element with the data-e2e-selector) asserts true. But the second (for the element with the ID - the checkbox next to the label) asserts false.

I know the problem is with my code, because the checkbox is indeed visible and manually checkable when stopping the test.

Ideas? Please advice if I need to post more details or better desciption.

Upvotes: 1

Views: 222

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

HTML for Attribute

The for attribute is an allowed attribute for <label> and <output>. When used on a <label> element it indicates the form element that this label describes.


Usage

When used as an attribute of <label>, the for attribute has a value which is the id of the form element it relates to:

<label for="username">Your name</label>
<input type="text" id="username">

Conclusion

Verifying the <label> element along with the visibility of for="innskuttegenkapital" attribute would be identical to verifying the <input> with id="innskuttegenkapital" attribute.

Upvotes: 1

Prophet
Prophet

Reputation: 33351

I guess the issue here is that the second element locator is not unique.
If so it is possible that Selenium finds some other element matching that locator and that element is not displayed.

Upvotes: 0

Related Questions