2012-07-17 192 views
0

我在Eclipse中使用selenium web驅動程序。我需要檢查表是否顯示在頁面上。我用這樣的代碼:檢查元素是否存在

try { 
    Assert.assertTrue(driver.findElement(By.xpath(".//*[@id='flexibleTable']")).isDisplayed()); 
} catch (AssertionError e) { 
    System.err.println("overview not found: " + e.getMessage()); 
} 

如果

if (driver.findElement(By.xpath(".//*[@id='flexibleTable']")).isDisplayed()) { 
    ... 
} else { 
    ... 
} 

但是,如果這樣的元素沒有找到測試中斷同時阻止。我如何組織檢查,以便即使元素不在頁面上,我的測試仍然繼續,否則塊會執行?

編輯 故障跟蹤

org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == .//*[@id='flexibleTable'] (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 360 milliseconds 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html 
Build info: version: '2.23.1', revision: '17143', time: '2012-06-08 18:59:04' 
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_04' 
Driver info: driver.version: RemoteWebDriver 
Session ID: c57bdd3e-ff76-43f8-9cd5-898248a4546a 
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:525) 
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188) 
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) 
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:458) 
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:226) 
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:311) 
at org.openqa.selenium.By$ByXPath.findElement(By.java:343) 
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:218) 
at system.Salutation.testUnit(Salutation.java:104) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我用硒2.23.1

+1

斯科特的答案是正確的。請注意,您還可以通過更好的方式通過id搜索元素:['findElement(By.id('flexibleTable'))'](http://selenium.googlecode.com/svn/trunk/docs/api/ java/org/openqa/selenium/By.html#id%28java.lang.String%29)或者,如果你更喜歡(slowe)xpath表達式:['findElement(By.xpath(「id('flexibleTable')」 ))'](http://www.w3.org/TR/xpath/#function-id) – 2012-07-17 15:01:39

+0

@Slanec是正確的,以儘可能避免xpath,它是相當緩慢 - 特別是在IE瀏覽器。 – Scott 2012-07-17 15:13:08

回答

-1

如果

String expression = ".//*[@id='flexibleTable']"; 
if (driver.findElement(By.xpath(expression) != null && 
    driver.findElement(By.xpath(expression)).isDisplayed()) { 
    ... 
} 
else { 
    ... 
} 
+0

我得到的類型WebDriver中的方法findElements(By)不適用於參數 (布爾值) – khris 2012-07-17 12:51:31

6

我在2.24.1同時阻止和異常層次結構可能已經改變了,但是當你撥打:

driver.findElement(By.xpath(".//*[@id='flexibleTable']")) 

如果元素未找到,將引發NoSuchElementException。該異常的父是NotFoundException,所以你可以修改代碼爲:

try { 
    WebElement e = driver.findElement(By.xpath(".//*[@id='flexibleTable']")); 
    Assert.assertTrue(e.isDisplayed()); 
} catch(NoSuchElementException nsee) { 
    System.out.println("The table was not located."); 
} catch(AssertionError ae) { 
    System.out.println("The table was located, but not displayed."); 
} 

這應該讓你的測試繼續進行。

+0

它不起作用在我的測試中,不知道爲什麼 – khris 2012-07-24 12:55:45

+0

你有堆棧跟蹤嗎?你在哪個版本的硒上? – Scott 2012-07-24 13:10:51