2

Basically I am searching for a syntax that I can use to select an element that shares the same name, properties and attributes.

I was thinking of selecting them via Index. (Unfortunately Xpath wont work since it's a dynamic element.)

So, I have a page where the element Add is shown twice, both of them adds/throws a different value. But both of them has the same ID, Attributes and Name. In my test I need to select the first Add and then the other.

${add attributes row}          //*[@data-bind="click: function() { 
$parents[1].addItem($parents[1]
    .attributes()[$parentContext.$index()]) }", index=1]


${add attributes row_2}     //*[@data-bind="click: function() { 
$parents[1].addItem($parents[1]
    .attributes()[$parentContext.$index()]) }", index=2]

Is there a way to select them by Index?

1 Answer 1

2

If you find an XPath that selects both of them, you can apply a predicate to the entire XPath by putting the XPath in parentheses. For instance, //a selects all anchors throughout the DOM. (//a)[4] selects the 4th anchor found in the DOM. You can also use the last and position functions to select relative indices such as the penultimate anchor (//a)[last()-1]

Try a locator like this for the 1st:

xpath=(//*[@data-bind="click: function() {$parents[1].addItem($parents[1].attributes()[$parentContext.$index()]) }"])[1]

Try a locator like this for the 2nd:

xpath=(//*[@data-bind="click: function() {$parents[1].addItem($parents[1].attributes()[$parentContext.$index()]) }"])[2]

See this related question

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

1 Comment

Thanks. Great stuff. that's the exact answer I was looking for.

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.