2014-04-27 88 views
0

我不清楚爲什麼我仍然得到這個Webdriver測試中引發的NoSuchElementException?即使當我發現NoSuchElementExcpetion時,它也會拋出?

任何人都可以調試或建議一個更有效率/ robyst解決方案嗎? !

NB - 我得到同樣的錯誤,如果我嘗試driver.findElements(By.id( 「...」)).size()= 0

boolean present; 
    try{ 
     driver.findElement(By.cssSelector(".message-bg-color>table>tbody>tr>td>h3")); 
     present = true; 
    }catch (NoSuchElementException e){ 
     present = false; 
    } 

    if(!present) { 

     // do some stuff 
    } 
    else 
    { 
     //do some otherstuff 
    } 

堆棧跟蹤:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":".message-bg-color>table>tbody>tr>td>h3"} 
Command duration or timeout: 1.17 seconds 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html 
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39' 
System info: host: 'L-IS08381', ip: '172.19.248.194', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_51' 
Session ID: 203c9fbc-3b59-4c6e-9f6b-03738b1c1c8f 
Driver info: org.openqa.selenium.firefox.FirefoxDriver 
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=28.0}] 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) 
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:437) 
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:425) 
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340) 
    at cms.workflow.DeleteArticleSysadminTest.deleteTestArtifactTest(DeleteArticleSysadminTest.java:109) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65) 
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"css selector","selector":".message-bg-color>table>tbody>tr>td>h3"} 
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39' 
System info: host: 'L-IS08381', ip: '172.19.248.194', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_51' 
+2

我想你應該分享一些你的代碼。檢查發生'NoSuchElementException'的特定行的錯誤堆棧。 – wns349

+0

你可能想查看一下[this](http://keysersblog.wordpress.com/2014/04/21/debugging-java-code-a-beginners-guide/)輕量級java調試教程(完全公開:我寫了它似乎符合您的需求) – keyser

+0

@ wns349 - 它被從附件中的第3行中拋出,即它試圖找到元素。 – Steerpike

回答

6

檢查您的密碼NoSuchElementExceptionorg.openqa.selenium.NoSuchElementException

很可能您已經使用java.util.NoSuchElementException代替。

您可以嘗試在源代碼頂部添加import org.openqa.selenium.NoSuchElementException

+0

謝謝 - 正確! 감사합니다 – Steerpike

相關問題