2

Selenium WebDriver と Java を使用して、次のアクションが実行される前に適切な要素がロードされていることを確認するために多くの待機が必要な自動テストを作成しています。

私はこれを試しました:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

指定された間隔で待機し、要素が見つからない場合は失敗します。

この:

WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(new ExpectedCondition<Boolean>() {
  public Boolean apply(WebDriver webDriver) {
    System.out.println("Searching for the Companies dropdown");
    return webDriver.findElement(By.id("ctl00_PageContent_vpccompanies_Input")) != null;
  }
});

要素が見つからない場合、無期限にハングします。

私が望むのは、要素を検索して数回試行し、エラーメッセージで失敗するものです。

4

2 に答える 2

1

コードをサイクルにラップし、検索条件が一致するか、ループを終了する追加の条件になるまでループします。isElementPresent(element)検索の条件を確認するために使用します。

于 2012-10-17T17:48:19.107 に答える
0

と言うと、your element access code whileループを挿入します。これは、またはのいずれかで中断しsuccessますnumber of attempts

(擬似コード)

    int numAttemps = 0;
    int specifiedAttempts = 5;
    boolean success = false;
    do{
       numAttemps++;
       try{
         //access the element
          WebElement element = driver.findElement(By.id(..));
          success  = true; //<--If it reaches here means success
       }catch(NoSuchElementException nse)
           //one attempt failed
       }
     }while(!success || numAttemps <specifiedAttempts);

     if(!success){
        System.out.println("Couldn't load after " +specifiedAttempts+ " attempts");
     }
于 2012-10-17T17:47:52.570 に答える