我在nav标签中有一个元素列表。我想点击第一个列表元素。我的HTML如下:
WebClient
我的测试案例有时失败了;我的测试用例通过但有时它失败了。 我使用 XVFB 在无头模式下运行我的测试用例。
应该在主题 * 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,但是我的测试用例失败了,我得到了一个异常。
答案 0 :(得分: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);;
希望它会对你有所帮助:)。