0

I have numerous elements in my ui tests that have the same css selector name but the issue I'm have is how to select each of them separately.

I recall there was a way to add [1],[2] after each of them but i cant seem to make it work.

I'm currently using "[data-qa-inning-value]"[1], "[data-qa-inning-value]"[2], etc but it is not picking it up, any help?

It currently isn't picking anything up but I do remember there was a way to do it but I just cant find it in my notes

1
  • Language being used is Java Commented Sep 10, 2019 at 9:43

1 Answer 1

1

Ideally, you should not hardcode the element number like [1] or [2] with the CSS Selector. This is because, many times, the dev team keeps adding or removing classes and structures which affect the DOM and you would lose track of your hardcoded elements resulting you in false positives.

Try to find elements uniquely. If CssSelector isn't helping, try XPath but keep the elements unique.

However if you still decide to choose the current approach, following can help you

After the class/id or the identifier, put :nth-of-type(1) or :nth-of-type(2) and so on to get your elements.

For example:

.c-primary-navigation__load-nav-item:nth-of-type(5)
Sign up to request clarification or add additional context in comments.

3 Comments

So if i want to use my selector it would be "[data-qa-inning-value:nth-of-type(1)]"?
you would not need square braces around. Try .data-qa-inning-value:nth-of-type(1). However, I am not sure if that is a class or id. Unless I see the DOM, can't say.
Hey the html is <p color="#DDDDDD" data-qa-inning-value="" class="sc-bdVaJa iUzeqM">runs O/U 8.5</p>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.