2017-03-16 51 views
0

我使用此代码来检查隐形:等待多个元素,成为无形的硒的Java

WebDriverWait wait = new WebDriverWait(driver,40); 
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(<some xpath>))); 

这完美的作品如果只有一个对应于网页中的XPath元素。

我有在我试图写一个脚本的网页,我需要的硒等待所有三个。

注意:我没有使用绝对xpath。

+0

你有3 webelement具有相同的XPath? – NarendraR

+0

是的,这个问题没有清楚吗? – XChikuX

回答

2

ExpectedConditions.invisibilityOfElementLocated检查第一个元素。在你的情况下,你可以编写自己的ExpectedCondition的实现,你必须检查是否为每个找到的元素显示对象。

为例(未测试):

private static void waitTillAllVisible(WebDriverWait wait, By locator) { 

    wait.until(new ExpectedCondition<Boolean>() { 

     @Override 
     public Boolean apply(WebDriver driver) { 

      Iterator<WebElement> eleIterator = driver.findElements(locator).iterator(); 
      while (eleIterator.hasNext()) { 
       boolean displayed = false; 
       try { 
        displayed = eleIterator.next().isDisplayed(); 
       } 
       catch (NoSuchElementException | StaleElementReferenceException e) { 
        // 'No such element' or 'Stale' means element is not available on the page 
        displayed = false; 
       } 
       if (displayed) { 
        // return false even if one of them is displayed. 
        return false; 
       } 
      } 
      // this means all are not displayed/invisible 
      return true; 
     } 
    }); 
} 
+0

看起来可能有效,但在调试时遇到麻烦。请修复“*没有类型参数*”和“*方法不能从超类*覆盖”错误 – XChikuX

+0

在我的机器上编译得很好。你使用的是哪个版本的java和selenium? –

+0

Java 1.8,Selenium 3.0.1 – XChikuX