1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

5 Comments

Wouldn't Selenium give a different error when there's multiple matches?
What exactly errors do you mean? Currently I can only guess since you didn't share a link of that page, I can't see what is going there.
Not errors, I think. If I remember correctly, when there are multiple elements with identical selector, the first one is found and the rest skipped. What I can say for sure is that this is not the case or the issue here.
In case there are multiple elements on the page matching the used locator selenium will pick the first matching element. So, I suggested this is what going in your case. The first matching element may be invisible while the other element (the one you hoped to get with Selenium) is visible, but this is not the element you actually picked and verified if it is displayed or not. In case this is not the case here - I don't know what is the issue here. You did not share a link to the page you are working on, not all your relevant code, so we can only guess...
Prophet: 1. The page is not on the internet. As it is with quite a lot of systems. 2. I think I shared all the relevant code. What do you think is missing? 3. undetected Selenium gave an answer that solved it, explaining something absout elements/DOM I didn't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.