Webdriver未检测到Web元素何时变为不可见

时间:2012-06-30 21:36:43

标签: selenium webdriver visibility

我无法使用“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);
}

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

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

改为使用 presenceOfElementLocated (通过定位器) 期望检查页面的DOM上是否存在元素。