'Xpath last element with certain class in Odoo

I'm trying to replace the last div element with the class 'test' in Odoo.

I tried several things such as:

 <xpath expr="//div[hasclass('test')][last()]">
 <xpath expr="//div[@class='test'][last()]">
 <xpath expr="//div[@class='test'][-1]">
 <xpath expr="(//div[@class='test'])[last()]">

But none of them seem to work. I don't know if this is even possible in Odoo v14. I researched the open source code, but sadly I didn't find anything that can help me out.



Solution 1:[1]

The first XPath is valid and should select the last div with test class.

According to the XPath documentation:

There is no limit to the number of predicates in a step, and they need not be confined to the last step in an XPath. They can also be nested to any depth.

You can find an example in the website module:

<xpath expr="//head/t[@t-js='false'][last()]" position="after">
    
</xpath>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kenly