# How to store xpath in a variable and then use for loop to iterate through each element in robot framework

**URL:** <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>\
**Category:** Robot Framework\
**Created:** [26 April 2023 18:46 UTC](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 "2023-04-26T18:46:18Z")\
**Posts on this page:** 11\
**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 18:46 UTC](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/1 "2023-04-26T18:46:18Z")

</div>

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\_index}]/div[1]/div/div

and then use For loop to iterate through each element how can I do that OR any other way to store xpath in a variable to use them like  
${xpath\_variable}[${row\_index}/${xpath\_variable2}[${col\_index}]/${xpath\_variable3}

Currently Im using it as

```
FOR ${row_index} IN RANGE 1 ${Count}+1
    FOR ${col_index} IN RANGE 1 ${subCount}+1
    ${text}= Get Text xpath= div[@class='lcp']/div[${row_index}/child::div[@class='row'][${col_index}]/div[1]/div/div
    END
END

```

Any suggestions How can I do that in robot framework

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [27 April 2023 02:23 UTC](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/2 "2023-04-27T02:23:14Z")

</div>

Hi Machindra,

I’ve done something like this before and it worked well for me:

```python
${xpprefix}= Set Variable div[@class='lcp']/div[${row_index}/child::div[@class='row']
FOR ${row_index} IN RANGE 1 ${Count}+1
    FOR ${col_index} IN RANGE 1 ${subCount}+1
    ${text}= Get Text xpath= ${xpprefix}[${col_index}]/div[1]/div/div
    END
END

```

You can adapt it as you need, e.g. with an ${xpsuffix}

Dave.

---

<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:** [27 April 2023 04:43 UTC](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/3 "2023-04-27T04:43:40Z")

</div>

```python
${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

```

Afer Set Variable it shows Log output of ${updated\_xpath} as P instead of //div[text()=‘Work Type’]/…/…/div/div/button[@disabled][1]  
I dont know why this is happening also i want to store the xpath in variable and for that I have diffrent objs file and in main code i want only variables to be used how can I do that any suggestions?

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [27 April 2023 05:55 UTC](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/4 "2023-04-27T05:55:55Z")

</div>

Hi Machindra,

Perhaps it’s my misunderstanding but I was under the impression that this syntax was only valid in the ` ***Variables*** ` section?

```python
${xpath}= //div[text()='Work Type']/../../div/div/button[@disabled]

```

When in a Test Case or a Keyword that you need to use a keyword that returns a variable like this:

```python
${xpath}= Set Variable //div[text()='Work Type']/../../div/div/button[@disabled]

```

So I’d suggest you try that first.

As for why you are getting a `P` I have no idea, but I’d suspect the interpreter is doing something weird with an unexpected syntax.

Dave.

---

<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:** [27 April 2023 10:59 UTC](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/5 "2023-04-27T10:59:52Z")

</div>

you undestood correct @damies13 xpath storedin seperate objs file  
ex. abc\_objs.robot  
` ***Variables*** `

```python
${xpath}= //div[text()='Work Type']/../../div/div/button[@disabled]

```

But when I used  
FOR ${row\_index} IN RANGE 1 ${elements. **len** ()}+1  
${updated\_xpath}= Get Text ${xpath}[${row\_index}]  
Log ${updated\_xpath}  
END  
It shows error as element with locator p not found it should be like//div[text()=‘Work Type’]/…/…/div/div/button[@disabled][1] #index position

any other best way to do that so that the code will also look good  
or can we store xpath in objs files and call them directly in robot file

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [27 April 2023 11:58 UTC](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/6 "2023-04-27T11:58:58Z")

</div>

Hi Machindra,

Now I’m finished work for the day had a bit of time to try it out, I took other libraries out of the equation and did a simple Robot Framework only test like this:

```python
***Variables***
${xpath}= //div[text()='Work Type']/../../div/div/button[@disabled]

***Test Cases***
Machindras xpath
	FOR ${row_index} IN RANGE 1 3+1
		${updated_xpath}= Set Variable ${xpath}[${row_index}]
		Log ${updated_xpath}
	END

```

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/d/dd7b2442c6642701eada86caed988b977a0fd97d.png)

When I saw that it was immediately obvious what was happening, in python when you place square brackets after a string variable with a number inside the result is that character is returned (see [Slicing Strings](https://www.w3schools.com/python/python_strings_slicing.asp)), so all you need to do is escape open the open square bracket like this:

```python
***Variables***
${xpath}= //div[text()='Work Type']/../../div/div/button[@disabled]

***Test Cases***
Machindras xpath
	FOR ${row_index} IN RANGE 1 3+1
		${updated_xpath}= Set Variable ${xpath}\[${row_index}]
		Log ${updated_xpath}
	END

```

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/8/83b79cb5a2b3d5543a85d035b6673b6f5635acc3.png)

Hopefully that solves your issue,

Dave.

---

<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:** [27 April 2023 12:34 UTC](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/7 "2023-04-27T12:34:07Z")

</div>

Thankyou so much @damies13 for your efforts .Its working fine now

---

<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:** [27 April 2023 13:38 UTC](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/8 "2023-04-27T13:38:53Z")

</div>

Example Test Case  
${xpprefix}= Set Variable div[@class=‘lcp’]/div[${row\_index}/child::div[@class=‘row’]  
FOR ${row\_index} IN RANGE 1 ${Count}+1  
FOR ${col\_index} IN RANGE 1 ${subCount}+1  
${text}= Get Text xpath= ${xpprefix}[${col\_index}]/div[1]/div/div  
END  
END

For this Itries to store ${xpprefix}= div[@class=‘lcp’]/div[${row\_index}/child::div[@class=‘row’]  
in objs file but text after [${row\_index} is not recognised can you tell me for this type of xpath how can I do ?

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [27 April 2023 14:05 UTC](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/9 "2023-04-27T14:05:42Z")

</div>

Hi Machindra,

Again escape the open square brackets, but also don’t forget to close them (after ${row\_index}):

You had this:

```python
${xpprefix}= Set Variable div[@class=‘lcp’]/div[${row_index}/child::div[@class=‘row’]

```

try it like this:

```python
${xpprefix}= Set Variable div[@class=‘lcp’]/div\[${row_index}]/child::div[@class=‘row’]

```

But also I’m not sure if that will work as you expect as the contents of `${row_index}` is uncertain at the time of `Set Variable`

So for a bit more certainty, it might be better to try it like this:

```python
	${xpthpre}= Set Variable div[@class=‘lcp’]/div
	FOR ${row_index} IN RANGE 1 ${Count}+1
		${xpthrow}= Set Variable ${xpthpre}\[${row_index}]/child::div[@class=‘row’]
		FOR ${col_index} IN RANGE 1 ${subCount}+1
			${text}= Get Text xpath=${xpthrow}\[${col_index}]/div\[1]/div/div
		END
	END

```

(watch out for the `div[1]/div/div` It might trip you up the same way → `div\[1]/div/div`)

Also a tip, when you put your code in the forum, use the triple back ticks (```) to prevent your spacing getting messed up like this:

```  
\< your code here \>  
```

Dave.

---

<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:** [27 April 2023 14:18 UTC](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/10 "2023-04-27T14:18:55Z")

</div>

Thanks for the efforts @damies13 this one is also working but I dont want any xpath on my main robot code file instead of xpath there should be only variables like

```python
 ${xpprefix}= Set Variable div[@class='lcp']/div\[${row_index}/child::div[@class='row']
FOR ${row_index} IN RANGE 1 ${Count}+1
    FOR ${col_index} IN RANGE 1 ${subCount}+1
    ${text}= Get Text xpath= ${xpprefix}\[${col_index}]/div[1]/div/div
    END
END

```

in the above code we can see the xpath in the code instead of this I want to store xpath in OBJS.robot file and use only variables here like

```python
 ${xpath}= Set Variable ${xpprefix}\[${col_index}]\${xpath2} 
${xpath2}= Get Text ${xpath}

```

Can we do this in robot framework OR we just have to use as above answer?

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [27 April 2023 21:54 UTC](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/11 "2023-04-27T21:54:33Z")

</div>

Hi Machindra,

Yes you can do this:

```python
${xpath2}= Get Text ${xpath}

```

Though you should probably name `${xpath2}` as something like `${value}` or `${test}` so you don’t get confused in future, as `Get Text` doesn’t return an xpath but rather the the text that the xpath resolves.

Dave.
