2017-03-16 52 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