Selenium WebDriver:确定元素是否可见的随机错误

时间:2012-09-26 14:27:27

标签: selenium webdriver selenium-webdriver

我正在使用Selenium 2.2。

我试图点击最初没有显示但会变得可见的元素 在测试期间。起初有时webdriver似乎运行得太快,因此Elements在时间上不可见,导致ElementNotVisibleExceptions。我添加了WebDriverWait以等待这些元素变得可见/可点击。但是现在我在使用

时遇到了这个随机错误
WebDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id")));

同样适用于

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

这是stacktrace

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可能会解决此问题,但这似乎不是一个干净的解决方案。它不能是超时问题,因为我尝试将超时限制设置为一分钟甚至更长时间,并且在几毫秒后仍然会失败。

任何人都有一个干净的解决方案吗?

提前致谢

编辑:顺便说一句。我正在使用InternetExplorerDriver

1 个答案:

答案 0 :(得分:0)

确保页面只有一个modalPanel。尝试获取可见面板(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;
                    }

                });