InesM
InesM

Reputation: 343

Selenium not finding element after click

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?

Upvotes: 1

Views: 475

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193088

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')]

Upvotes: 4

Related Questions