首页 文章

无法使用selenium webdriver在<nav>和<ul>标签中单击列表元素

提问于
浏览
2

我在nav标签中有一个元素列表 . 我想点击第一个列表元素 . 我的HTML如下:

<aside id="left-panel" style="overflow: visible;">
   <div id="selectedBrand" class="custum-brand-info login-info">
      <span class="dropdown">
    </div>
    <nav>
       <ul id="sideNavigation">
          <li>
             <a onclick="showHideBrandmanagement(true);" href="/sentiment/view">
                <i class="fa fa-lg fa-fw fa-thumbs-up"></i>
                <span class="menu-item-parent">Sentiment</span>
             </a>
          </li>
       </ul>
    </nav>

我的测试案例有时失败了;我的测试用例通过但有时它失败了 . 我使用 XVFBheadless 模式下运行我的测试用例 .

应该在主题 *** FAILED *** 中获取数据

[info]   org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#left-panel nav #sideNavigation li:nth-child(9) #rAnalytics"}
[info] Command duration or timeout: 10.06 seconds
[info] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
[info] Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
[info] System info: host: 'knoldus-Vostro-2520', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.19.0-30-generic', java.version: '1.7.0_80'
[info] *** Element info: {Using=css selector, value=#left-panel nav #sideNavigation li:nth-child(9) #rAnalytics}
[info] Session ID: edc0a525-38bb-48b2-88c4-e1ad01265c15
[info] Driver info: org.openqa.selenium.firefox.FirefoxDriver
[info] Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=41.0}]
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[info]   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[info]   at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[info]   at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
[info]   at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
[info]   at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
[info]   at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
[info]   at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:437)
[info]   at selenium.professionalPlan.Topic$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(Topic.scala:31)
[info]   ...
[info]   Cause: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"#left-panel nav #sideNavigation li:nth-child(9) #rAnalytics"}
[info] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
[info] Build info: version: '2.47.1', revision: 'unknown', time: '2015-07-30 11:02:44'
[info] System info: host: 'knoldus-Vostro-2520', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.19.0-30-generic', java.version: '1.7.0_80'
[info] Driver info: driver.version: unknown
[info]   at .FirefoxDriver.prototype.findElementInternal_(file:///tmp/anonymous5218088375039490898webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10667)
[info]   at .fxdriver.Timer.prototype.setTimeout/<.notify(file:///tmp/anonymous5218088375039490898webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:623)
[1]

我试过Xpath,cssSelector,甚至我手动为第一个锚标记提供了一个id,但我的测试用例失败了,我得到了一个例外 .

1 回答

  • 0

    如果你想找到一个id然后使用

    //nav[1]/ul[@id='sideNavigation']
    

    但是在xpath之上会返回许多元素 .

    如果您正在寻找包含 "Sentiment" 文本的元素,那么请使用以下Xpath: -

    //nav[1]/ul[@id='sideNavigation']//span[contains(text(),'Sentiment')]
    

    如果仍然无法正常工作,请检查您的页面中是否有任何框架 . 如果该帧包含上述元素的HTML代码,则需要先切换到帧

    请尝试使用以下代码: -

    WebElement elem = driver.findElement(By.xpath("//nav[1]/ul[@id='sideNavigation']//span[contains(text(),'Sentiment')]"));
    String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
    
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript(js, elem);;
    

    希望它能帮到你:)

相关问题