在Selenium Java中找不到标签元素

时间:2019-02-28 04:15:26

标签: selenium selenium-webdriver

我无法在我的网站中找到标签元素。以下是tab元素的html代码:

<a class="tab-button has-title has-icon" href="#" role="tab" id="tab-t0-2" aria-controls="tabpanel-t0-2" aria-selected="true">
    <custom-icon class="mmi-tabbar-tpr tab-button-icon"></custom-icon>
    <!---->
    <!----><span class="tab-button-text">TPR</span>
    <!---->
    <div class="button-effect" style="transform: translate3d(-14px, -17px, 0px) scale(1); height: 103px; width: 103px; opacity: 0; transition: transform 313ms, opacity 219ms 94ms;"></div>
</a>

它也不在任何iframe中。我尝试复制其xpath //*[@id="tab-t0-2"],但是没有运气。任何帮助都将是可贵的。

记录的错误:

  

线程“主要” org.openqa.selenium.NoSuchElementException中的异常:   没有这样的元素:无法找到元素:   {“ method”:“ xpath”,“ selector”:“ // a [@ id ='tab-t0-2']”}}(会话信息:   chrome = 61.0.3163.100)(驱动程序信息:chromedriver = 2.34.522940   (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform = Windows NT   6.1.7601 SP1 x86_64)(警告:服务器未提供任何堆栈跟踪信息)

2 个答案:

答案 0 :(得分:0)

如果ID不是动态的,您可以尝试使用XPath:

//a[@id='tab-t0-2']

如果ID是动态的,请使用:

//a[normalize-space()='TPR']

答案 1 :(得分:0)

您可以尝试使用此xpath

通过= By.xpath(“ // span [。='TPR']” )或

By.cssSelector(“ div [class ='button-effect']” ))

让我知道它是否有效。