Eugen
Eugen

Reputation: 805

Find element based on a property other than id

Sorry for the simplicity of the question, or for the clumsiness, but I'm just starting now to use Testcafe/JavaScript.

Now the problem: - I want to input a value, in a field, for which the id is dynamically allocated at every browser refresh. - because the id changes every time, I have to call find the object after some other property Question: how can I do this, in Testcafe?

Testcafe proposes this code:

"2.Type in input": function() {
    act.type("#6287a1e6-prefix", "030");

and the property of the object are those: enter image description here

Thank you in advance

Upvotes: 2

Views: 601

Answers (2)

Shubham Gupta
Shubham Gupta

Reputation: 2646

Assuming the field name is Prefix you can following two methods to get the elements.

var elements = document.getElementsByName("Prefix")

which will return NodeList (if element not found will return empty NodeList), you can access elements like elements[0] etc.

OR

document.querySelector("input[name=Prefix]")

If element not found will return null.

Upvotes: 2

Barmar
Barmar

Reputation: 780798

You can use a selector to find it based on the name attribute:

document.querySelector("input[name=Prefix]")

Upvotes: 4

Related Questions