gpt4 book ai didi

java - 检查元素是否存在

转载 作者:行者123 更新时间:2023-12-01 17:30:30 31 4
gpt4 key购买 nike

我在 Eclipse 中使用 selenium web-driver。我需要检查表格是否显示在页面上。我使用这样的代码:

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 {
...
}

但是如果没有找到这样的元素,测试就会中断。我如何组织检查,以便即使元素不在页面上,我的测试也会继续,并且其他 block 将执行?

编辑故障追踪

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)

我使用 Selenium 2.23.1

最佳答案

我使用的是 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.");
}

这应该允许您的测试继续。

关于java - 检查元素是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11522269/

31 4 0
文章推荐: java - 如何计算一个点距另一个点一定距离的纬度/经度?
文章推荐: java - NavigableSet 与方法参数中的 Collection 不匹配