2012-09-26 67 views
4

我使用的是Selenium 2.2。Selenium WebDriver:随机错误确定元素是否可见

我试图点击最初没有显示的元素,但在测试过程中变为可见的 。起初有时webdriver似乎工作得太快,所以元素不及时显示,导致ElementNotVisibleExceptions。我添加了WebDriverWait以等待这些元素变为可见/可点击。但现在我使用

WebDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id"))); 

同为

WebDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("id"))); 

这里的时候得到这个随机误差是堆栈跟踪

org.openqa.selenium.WebDriverException: Error determining if element is displayed (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 219 milliseconds Build info: version: '2.20.0', revision: '16008', time: '2012-02-27 19:03:59' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1 build 2600 Service Pack 3', java.version: '1.6.0' Driver info: driver.version: RemoteWebDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:44) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:516) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:170) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:123) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:438) at org.openqa.selenium.remote.RemoteWebElement.isDisplayed(RemoteWebElement.java:280) at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:136) at org.openqa.selenium.support.ui.ExpectedConditions.access$1(ExpectedConditions.java:135) at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:106) at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1) at org.openqa.selenium.support.ui.ExpectedConditions$11.apply(ExpectedConditions.java:252) at org.openqa.selenium.support.ui.ExpectedConditions$11.apply(ExpectedConditions.java:1) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:201) at MyTest.myTest(MyTest.java:xx) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:600) at junit.framework.TestCase.runTest(TestCase.java:168) at junit.framework.TestCase.runBare(TestCase.java:134) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:243) at junit.framework.TestSuite.run(TestSuite.java:238) at junit.framework.TestSuite.runTest(TestSuite.java:243) at junit.framework.TestSuite.run(TestSuite.java:238) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

这只有时会发生。忽略WebDriverException可能会解决这个问题,但这似乎不是一个干净的解决方案。它不能是超时问题,因为我尝试将超时限制设置为一分钟或更长时间,并且在几毫秒后仍然失败。

有人得到了一个干净的解决方案呢?

在此先感谢

编辑:btw。我正在使用InternetExplorerDriver

+1

2.2是积极的古老。如果你使用最新版本会发生什么?如果仍然失败,那么下一个问题将是:“您能提供代码,包括要测试的HTML页面,是否会重现此问题?” – JimEvans

+0

它仍然发生在最新版本上。不幸的是,我无法提供实际的代码,但它基本上只是一个richfaces rich:modalpanel,它将在单击jsf a4j:commandbutton后显示。之后,我想点击另一个a4j:在rich:modalpanel上的命令按钮。它会失败,确定模式面板上的命令按钮的可见性 – Robinho

+0

你解决了你的问题吗? – zluis0

回答

0

确保页面只有一个模式面板。尽量让可视面板(JAVA):

public WebElement getVisibleModalPanel(){ 
for (WebElement element : driver.findElements(By.cssSelector('csslocator'))) { 
    if (element.isDisplayed()) { 
     return element; 
    } 
} 
return null; 
} 

器等这样的事情:

(new WebDriverWait(getDriver(), timeout, 400)).ignoring(StaleElementReferenceException.class).until(
       new ExpectedCondition<Boolean>() { 

        @Override 
        public Boolean apply(WebDriver driver) { 
         for (WebElement element : driver.findElements(By.cssSelector(locator))) { 
          if (element.isDisplayed()) { 
           return true; 
          } 
         } 
         return false; 
        } 

       }); 
相关问题