# How to use variables in xpath

**URL:** <https://forum.robotframework.org/t/how-to-use-variables-in-xpath/5612>\
**Category:** Robot Framework\
**Created:** [26 April 2023 20:33 UTC](https://forum.robotframework.org/t/how-to-use-variables-in-xpath/5612 "2023-04-26T20:33:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk111](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mk111](https://forum.robotframework.org/u/mk111)\
**Post date:** [26 April 2023 20:33 UTC](https://forum.robotframework.org/t/how-to-use-variables-in-xpath/5612/1 "2023-04-26T20:33:22Z")

</div>

```python
After set varible it shows as p instead of //div[text()='Work Type']/../../div/div/button[@disabled][1] 
   ${xpath}= //div[text()='Work Type']/../../div/div/button[@disabled]
    ${elements}= Get WebElements ${xpath}
    FOR ${row_index} IN RANGE 1 ${elements. __len__ ()}+1
        ${updated_xpath}= Set Variable ${xpath}[${row_index}]
        Log ${updated_xpath}
        ${text}= Get Text ${updated_xpath}
    END

```

why Afer Set Variable it shows Log output as p instead of //div[text()=‘Work Type’]/…/…/div/div/button[@disabled][1]

---

<div class="post-metadata">

**Author:** ![\_daryl](https://avatars.discourse-cdn.com/v4/letter/_/45deac/32.png) [@\_daryl](https://forum.robotframework.org/u/_daryl)\
**Post date:** [28 April 2023 14:18 UTC](https://forum.robotframework.org/t/how-to-use-variables-in-xpath/5612/2 "2023-04-28T14:18:13Z")

</div>

Looks to be a Duplicate of:

> [@How to store xpath in a variable and then use for loop to iterate through each element in robot framework](https://forum.robotframework.org/t/how-to-store-xpath-in-a-variable-and-then-use-for-loop-to-iterate-through-each-element-in-robot-framework/5610):
>
> Actually, I have an xpath that is stored in a variable that has multiple matching xpaths. I want to store that xpath in a variable and iterate through each element using a for loop and do some code on every element one by one. How can I do that? Currently i have xpath as xpath= div[@class=‘lcp’]/div[${row\_index}/child::div[@class=‘row’][${col\_index}]/div[1]/div/div I want to store this xpath in a variable like ${xpath}= div[@class=‘lcp’]/div[${row\_index}/child::div[@class=‘row’][${col\_inde…
