1

I'm using Selenium with Robot Framework. My web application is written in React. I'm having a problem because my Selenium test does not find an element after I click a link.

I tried to add a sleep to check if Selenium was just being too quick and not detecting the change after, but that doesn't seem to be the case. Also, I tried to add a very large sleep and trying to find the element in the browser console and in the console the element can be found.

Code trials:

Click Link //li/a[contains(., /span[@class='nav-text']/span[.='Element'])] Page Should Contain Element //li[@class='ant-menu-item ant-menu-item-selected']/a/span/span[.='Element'] 

HTML:

<div class="ant-layout-sider-children"> <div class="logo"></div> <ul class="ant-menu ant-menu-dark ant-menu-root ant-menu-inline" role="menu"> <li class="ant-menu-item" role="menuitem" style="padding-left: 24px;"> <a href="/elements"> <span class="nav-text"><span>Elements</span></span> </a> </li> <li class="ant-menu-item ant-menu-item-selected" role="menuitem" style="padding-left: 24px;"> <a class="active" aria-current="page" href="/parameters"> <span class="nav-text"><span>Parameters</span></span> </a> </li> </ul> </div> 

Error:

Page should have contained element '//li[@class='ant-menu-item ant-menu-item-selected']/a/span/span[.='Elements']' but did not 

When an item is clicked the ant-menu-item-selected changes to the selected <li>.

Can someone help me with this?

0

1 Answer 1

4

To find the element after it is clicked you can use the following solution:

Click Link //ul[@class='ant-menu ant-menu-dark ant-menu-root ant-menu-inline']//li[contains(@class, 'ant-menu-item')]/a/span[@class='nav-text']/span[contains(., 'Elements')] Page Should Contain Element //ul[@class='ant-menu ant-menu-dark ant-menu-root ant-menu-inline']//li[contains(@class, 'ant-menu-item-selected')]/a/span[@class='nav-text']/span[contains(., 'Elements')] 
Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfectly. Thank you so much. Can you explain why my solution wasn't working?
The approach in my solution was to drill down the <ul> and locate the required <li> node with class as attribute containing the value ant-menu-item and the text Elements. Once clicked the same element changes to contain the class attribute as ant-menu-item-selected. Your code trials were near perfect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.