单击后硒找不到元素

时间:2019-01-04 11:38:02

标签: selenium selenium-webdriver xpath webdriver robotframework

我正在将Selenium与Robot Framework一起使用。我的Web应用程序是用React编写的。我遇到了问题,因为单击链接后我的Selenium测试找不到元素。

我试图添加一个睡眠来检查Selenium是否太快并且在之后没有检测到更改,但是事实并非如此。另外,我尝试添加一个很大的睡眠,并尝试在浏览器控制台中找到该元素,并且可以在控制台中找到该元素。

代码试用:

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>

错误:

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

单击某项后,ant-menu-item-selected会变为所选的<li>

有人可以帮我吗?

1 个答案:

答案 0 :(得分:3)

要在单击元素后查找元素,可以使用以下解决方案:

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