Gheeno San Pascual III
Gheeno San Pascual III

Reputation: 136

Robotframework selecting an element by index

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?

Upvotes: 2

Views: 5093

Answers (1)

ombre42
ombre42

Reputation: 2384

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

Upvotes: 2

Related Questions