# How to iterate through child elements of a webelement?

**URL:** <https://forum.robotframework.org/t/how-to-iterate-through-child-elements-of-a-webelement/7610>\
**Category:** SeleniumLibrary\
**Created:** [22 July 2024 11:26 UTC](https://forum.robotframework.org/t/how-to-iterate-through-child-elements-of-a-webelement/7610 "2024-07-22T11:26:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmathur87](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/vmathur87/32/4090_2.png) [@vmathur87](https://forum.robotframework.org/u/vmathur87)\
**Post date:** [22 July 2024 11:26 UTC](https://forum.robotframework.org/t/how-to-iterate-through-child-elements-of-a-webelement/7610/1 "2024-07-22T11:26:13Z")

</div>

I have a list of users on a webpage like this

```python
<div id="users">
    <li class="user_list">
        <div class="users-name">User One</div>
        <div class="other details">...</div>
    </li>
    <li class="user_list">
        <div class="users-name">User Two</div>
        <div class="other details">...</div>
    </li>
</div>

```

I want to iterate through the list and get the names of the users like this

```python
@{users_list}= Get WebElements xpath=//li[contains(@class, 'user_list')]
FOR ${user} IN @{users_list}
    ${name_el}= Get WebElement xpath=${user}/div[@class='users-name']
    ${name_text}= Get Text ${parent}
    Log Name= ${name_text}
END

```

But I am getting a syntax error that this is not a valid Xpath expression. How can I resolve this? I am using robotframework v7.0 with seleniumlibrary 6.0.

---

<div class="post-metadata">

**Author:** ![dgalezow](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@dgalezow](https://forum.robotframework.org/u/dgalezow)\
**Post date:** [22 July 2024 11:41 UTC](https://forum.robotframework.org/t/how-to-iterate-through-child-elements-of-a-webelement/7610/2 "2024-07-22T11:41:16Z")

</div>

You can log `${user}` to see it no longer contains xpath, but internal selenium pointer to an element.

Why not change to something like this?

````python
@{users_list}= Get WebElements xpath=//li[contains(@class, 'user_list')]/div[@class='users-name']
FOR ${user} IN @{users_list}
    ${name_text}= Get Text ${user}
    Log Name= ${name_text}
END```
````

---

<div class="post-metadata">

**Author:** ![vmathur87](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/vmathur87/32/4090_2.png) [@vmathur87](https://forum.robotframework.org/u/vmathur87)\
**Post date:** [22 July 2024 14:42 UTC](https://forum.robotframework.org/t/how-to-iterate-through-child-elements-of-a-webelement/7610/3 "2024-07-22T14:42:51Z")

</div>

Later on I also need to click on ${name\_el}. Only ${name\_el} is clickable, ${user} is not.

---

<div class="post-metadata">

**Author:** ![dgalezow](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@dgalezow](https://forum.robotframework.org/u/dgalezow)\
**Post date:** [22 July 2024 15:21 UTC](https://forum.robotframework.org/t/how-to-iterate-through-child-elements-of-a-webelement/7610/4 "2024-07-22T15:21:10Z")

</div>

Hmm, you could use [Get Element Count](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Get%20Element%20Count) and iterate through your original xpath but add `[${i}]` at the end.

```python
${users_count}= Get Element Count xpath=//li[contains(@class, 'user_list')]

FOR %{i} IN RANGE ${users_count} 
    ${name_text}= Get Text xpath=(//li[contains(@class, 'user_list')]/div[@class='users-name'])[%{i}]
    Log Name= ${name_text}
END

```

Would something like that work for you?

---

<div class="post-metadata">

**Author:** ![vmathur87](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/vmathur87/32/4090_2.png) [@vmathur87](https://forum.robotframework.org/u/vmathur87)\
**Post date:** [23 July 2024 18:51 UTC](https://forum.robotframework.org/t/how-to-iterate-through-child-elements-of-a-webelement/7610/5 "2024-07-23T18:51:45Z")

</div>

Thanks for the suggestion…

I finally figured I could iterate on the users-name instead of the user\_list, so I ended up doing something like this

```python
@{users_names_list}= Get WebElements xpath=//div[@class='users-name']
FOR ${i} IN RANGE ${users}
	${user_name}= Get Text ${users_names_list}[${i}]
	Log	${user_name}
	IF	'${user_name}' == '{my_username}'
		${user_name_el}=	Get WebElement	${users_names_list}[${i}]
		Click Element ${user_name_el}
		BREAK
	END
END

```
