2

我无法让这个“invisibilityOfElementLocated()”方法工作。这是我正在使用的代码,我亲眼目睹该元素在大约 35 秒后消失,但 Webdriver 没有检测到这一点并在此代码上崩溃并跳过测试。如果有人有更好的不同方法,我会很高兴。

if ( driver.findElement( By.xpath( "//div[@class='Caption']" )).isDisplayed() ) {
  System.out.println("Caption is visible.");
}
WebDriverWait wait1 = new WebDriverWait( driver , 60 ); 
wait1.until( 
  ExpectedConditions.invisibilityOfElementLocated( 
    By.xpath( "//div[@class='Caption']" ) 
  )
);

这是我导致的错误:

org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for
  element to no longer be visible: By.xpath: //div[@class='Caption']
Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 16:53:24'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', 
  java.version: '1.6.0_31'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(
          FluentWait.java:251)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:220)
    at test.TaskListPage.isLoaded(TaskListPage.java:43)
    at org.openqa.selenium.support.ui.SlowLoadableComponent.get(
         SlowLoadableComponent.java:48)
    at test.TaskListPage.<init>(TaskListPage.java:31)
    at test.Form.testLogin(Form.java:153)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
        NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
           DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    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.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    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.internal.runners.statements.RunAfters.evaluate(
       RunAfters.java:30)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)

此外,此方法给出了类似的错误:

ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
  public Boolean apply(WebDriver d) {
    WebElement tE = driver.findElement( By.className("Caption") );
    // return the invisibility instead of the visibility
    return !tE.isDisplayed();
  }
};
WebDriverWait w = new WebDriverWait(driver, 60);
w.until(e); 

我在这方面取得了一些成功,但是当元素最终变得不可见然后将我从测试用例中弹出时,它仍然会引发异常:

while ( 
  // dialogMiddleCenterInner is a td that contains the Caption div
  driver.findElement( By.className("dialogMiddleCenterInner")
).isDisplayed() ) {
  System.out.println("Caption is visible.  Waiting for login.");
  AFormUtils.waitSeconds(2);
}
4

2 回答 2

1

感谢 Slanec 试图帮助我。好的,这就是答案。看来,无论我是否使用 ExpectedCondition,我都只需要处理异常而不是让它抛出,但它最终起作用了:

try {
  while ( 
    // dialogMiddleCenterInner is a td that contains the Caption div
    driver.findElement( By.className("dialogMiddleCenterInner")
  ).isDisplayed() ) {
  System.out.println("Caption is visible.  Waiting for login.");
  MyUtils.waitSeconds(2);
} catch ( NoSuchElementException nse ) {
  nse.printStackTrace(); // print but simulate .ignoring() by catching exception
}
于 2012-07-01T01:38:11.817 回答
0

invisibilityOfElementLocated(通过定位器)检查元素是否不可见或不存在于 DOM 上的期望。

改为使用 presenceOfElementLocated (By locator) 检查页面 DOM 上是否存在元素的期望。

于 2013-07-31T08:26:37.140 回答