# Help locating element

**URL:** <https://forum.robotframework.org/t/help-locating-element/1313>\
**Category:** Robot Framework\
**Created:** [9 February 2021 18:10 UTC](https://forum.robotframework.org/t/help-locating-element/1313 "2021-02-09T18:10:05Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [9 February 2021 18:10 UTC](https://forum.robotframework.org/t/help-locating-element/1313/1 "2021-02-09T18:10:05Z")

</div>

Hi,

I want to verify that class has highlighted a table row background clicked.

This is my row: `xpath=.//*[@id="my-row-0"]/td[3]/div[2]/div[1] `This is the class that is either off (not clicked) `contains(@class, 'my-graphic-link body-row not-selected')] or on contains(@class, 'my-graphic-link body-row is-selected')] `

I’m using `Wait until page contains element` after row is click but just get errors.

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![hobet](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hobet](https://forum.robotframework.org/u/hobet)\
**Post date:** [9 February 2021 18:30 UTC](https://forum.robotframework.org/t/help-locating-element/1313/2 "2021-02-09T18:30:58Z")

</div>

What are the errors you are getting? Is this using SeleniumLibrary? A web app or something else?

---

<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:** [9 February 2021 23:05 UTC](https://forum.robotframework.org/t/help-locating-element/1313/3 "2021-02-09T23:05:32Z")

</div>

Hi Brendan,

It’s not clear if you mean that the TD element (//_[@id=“my-row-0”]/td[3]) or the DIV element (//_[@id=“my-row-0”]/td[3]/div[2]/div[1]) is the one that gets the changed class? I would expect in a web app the class to be applied to the TD elements, but I have seen developers do some pretty wacky things.

You also didn’t show your robot script so it’s a bit hard for us to know what you already have but one of these might work, i’m just guessing as there is not enough info to do anything else:

`Wait until page contains element | //*[@id="my-row-0"]/td[3]/div[2]/div[contains(@class, "is-selected")]`  
`Wait until page contains element | //*[@id="my-row-0"]/td[contains(@class, "is-selected")]`

But those will be true if any TD or DIV (depending on which you used) has the is-selected class.

Another way you could approach it would be to collect the class attribute from the required element and then test the contents of the class, e.g.

`${class}= | Get Element Attribute //*[@id="my-row-0"]/td[3]/div[2]/div[1]`  
or  
`${class}= | Get Element Attribute //*[@id="my-row-0"]/td[3]`

Followed by a simple  
`Should Contain | ${class} | is-selected`

Especially for the first selected item and if there is a delay between clicking a checkbox and the style being applied you might need to combine both methods, 1st wait for any element of the type your expecting to have the is-selected class, then test the specific element has the class.

Hopefully this helps,

Dave.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [10 February 2021 08:33 UTC](https://forum.robotframework.org/t/help-locating-element/1313/4 "2021-02-10T08:33:30Z")

</div>

Thanks for your help. I am using Selenium library and running the tests in RIDE. This is my error:

```
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression .//*[@class="my-graphic-link body-row is-selected"]/td[3]/div[2]/div[1] contains(@class, 'my-graphic-link body-row is-selected')] because of the following error:

```

SyntaxError: Failed to execute ‘evaluate’ on ‘Document’: The string ‘.//\*[@class=“my-graphic-link body-row is-selected”]/td[3]/div[2]/div[1] contains(@class, ‘my-graphic-link body-row is-selected’)]’ is not a valid XPath expression.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [10 February 2021 08:37 UTC](https://forum.robotframework.org/t/help-locating-element/1313/5 "2021-02-10T08:37:55Z")

</div>

When the user presses the table row only at that point is `is-selected`` added to the class. So my logic was that after row click search page for that class to be visible which is:

```
Click Element //*[@id="my-row-0"]
Wait For Load Finish
Wait until page contains element xpath=.//*[@class="my-graphic-link body-row **is-selected**"]/td[3]/div[2]/div[1] contains(@class, 'my-graphic-link body-row **is-selected**')]
```

---

<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:** [10 February 2021 09:51 UTC](https://forum.robotframework.org/t/help-locating-element/1313/6 "2021-02-10T09:51:32Z")

</div>

Hi Brendan,

Are you able to show us a snippet of the html you are trying to use the xpath on before and after selecting the row? Then I might have a better idea why it’s not working for you.

Dave.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [10 February 2021 17:34 UTC](https://forum.robotframework.org/t/help-locating-element/1313/7 "2021-02-10T17:34:05Z")

</div>

```
<tr class="my-graphic-link body-row is-selected" id="my-row-0">

```

The “is-selected” only shows when row clicked… so this is taken from a clicked state.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [10 February 2021 17:35 UTC](https://forum.robotframework.org/t/help-locating-element/1313/8 "2021-02-10T17:35:15Z")

</div>

I need to be able to verify that the row is still in a “highlightable” state.

---

<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:** [11 February 2021 23:36 UTC](https://forum.robotframework.org/t/help-locating-element/1313/9 "2021-02-11T23:36:29Z")

</div>

can you add these 2 lines to your test case and show the result?:

```python
${class}= | Get Element Attribute //tr[@id="my-row-0"]
Should Contain | ${class} | is-selected

```

I think that will work, from what you were describing earlier you were trying to check the is-selected on the td or div elements inside the tr, but it now looks like it’s the tr you needed to check

Hopefully this works,

Dave.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [13 February 2021 14:05 UTC](https://forum.robotframework.org/t/help-locating-element/1313/10 "2021-02-13T14:05:22Z")

</div>

Thanks, I’m getting an error when running code:

```
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//tr[(' is not a valid XPath expression.

 Verify Highlightable Areas
Click Element //*[@id="my-row-0"]
${class}= Get Element Attribute //tr[@id="my-row-0"]
Page Should Contain ${class} is-selected
```

---

<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:** [14 February 2021 07:04 UTC](https://forum.robotframework.org/t/help-locating-element/1313/11 "2021-02-14T07:04:46Z")

</div>

Hi Brendan,

Are you sure that failure is on these lines? the reason I ask is the error message says ‘//tr[(’ is not valid xpath, which is true but none of these lines have the ‘(’ character, so it looks like that error is coming from somewhere else.

Dave.

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [14 February 2021 18:07 UTC](https://forum.robotframework.org/t/help-locating-element/1313/12 "2021-02-14T18:07:34Z")

</div>

Hi Dave,

I ran the code with and without “( )” to see if it made a difference and got the same error.

---

<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:** [15 February 2021 08:12 UTC](https://forum.robotframework.org/t/help-locating-element/1313/13 "2021-02-15T08:12:32Z")

</div>

Unfortunately you aren’t providing enough information for me to figure out what is going on, perhaps someone else will chime in and can help you?

---

<div class="post-metadata">

**Author:** ![resourceful-idiot](https://avatars.discourse-cdn.com/v4/letter/r/ba9def/32.png) [@resourceful-idiot](https://forum.robotframework.org/u/resourceful-idiot)\
**Post date:** [17 February 2021 21:52 UTC](https://forum.robotframework.org/t/help-locating-element/1313/14 "2021-02-17T21:52:40Z")

</div>

Hi Dave,

I fixed this by using `Page Should Contain Element [(@class, ‘my-graphic-link body-row not-selected’)] and contains(@class, ‘is-selected’)]

Thank you for your help, much appreciated.

`

---

<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:** [17 February 2021 21:58 UTC](https://forum.robotframework.org/t/help-locating-element/1313/15 "2021-02-17T21:58:42Z")

</div>

Great! I’m glad you solved it 👍🏼
