22

WebDriverWait を使用して、属性が変更されるまで待機するにはどうすればよいですか?

私の AUT では、続行する前にボタンが有効になるまで待つ必要があります。残念ながら、開発者がページをコーディングした方法が原因で、WebElement の isEnabled() メソッドを使用できません。開発者は CSS を使用してボタンを無効にしているように見せているため、ユーザーはボタンをクリックできず、メソッド isEnabled は常に true を返します。したがって、属性「aria-disabled」を取得し、テキストが「true」か「false」かを確認する必要があります。私がこれまで行ってきたことは、次のような Thread.sleep を使用した for ループです。

for(int i=0; i<6; ++i){
    WebElement button = driver.findElement(By.xpath("xpath"));
    String enabled = button.getText()
    if(enabled.equals("true")){ break; }
    Thread.sleep(10000);
 }

(間違っている場合は上記のコードを無視してください。私がやっていることの単なる疑似コードです)

WebDriverWait を使用して同様のことを達成する方法があると確信していますが、これは方法がわかりません。これは私が達成しようとしているものですが、以下はうまくいきません:

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(refresh.getText() == "true")); 

関数は String ではなく WebElement を期待しているため、明らかに機能しませんが、それは私が評価しようとしているものです。何か案は?

4

5 に答える 5

36

以下は、要件に役立つ場合があります。次のコードでは、探している条件を組み込んだ apply メソッドをオーバーライドします。そのため、条件が true でない限り、この場合、enabled は true ではありません。apply メソッドが true を返すまで、最大 10 秒間ループし、500 ミリ秒ごとにポーリングします (これがデフォルトです)。

WebDriverWait wait = new WebDriverWait(driver,10);

wait.until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver driver) {
        WebElement button = driver.findElement(By.xpath("xpath"));
        String enabled = button.getAttribute("aria-disabled");
        if(enabled.equals("true")) 
            return true;
        else
            return false;
    }
});
于 2013-03-06T02:36:36.653 に答える
2

誰かが @Sri を Selenium ラッパーのメソッドとして使用したい場合は、次の方法があります (この回答のおかげで):

public void waitForAttributeChanged(By locator, String attr, String initialValue) {
    WebDriverWait wait = new WebDriverWait(this.driver, 5);

    wait.until(new ExpectedCondition<Boolean>() {           
        private By locator;
        private String attr;
        private String initialValue;

        private ExpectedCondition<Boolean> init( By locator, String attr, String initialValue ) {
            this.locator = locator;
            this.attr = attr;
            this.initialValue = initialValue;
            return this;
        }

        public Boolean apply(WebDriver driver) {
            WebElement button = driver.findElement(this.locator);
            String enabled = button.getAttribute(this.attr);
            if(enabled.equals(this.initialValue)) 
                return false;
            else
                return true;
        }
    }.init(locator, attr, initialValue));
}
于 2014-05-20T14:03:29.927 に答える
0

ソートボタンが有効になるのを待っているかなり遅いブートストラップページに対して自動化するとき、これは私にとってはうまくいきました。

    new WebDriverWait(webDriver, 10)
.until(ExpectedConditions.attributeContains(BUTTON_SORT_ASCENDING_PRICE, "class", "sortButtonActive"));
  • "webDriver" - WebDriver の現在のインスタンス
  • "10" - タイムアウト秒
  • "BUTTON_SORT_ASCENDING_PRICE" - 要素の By ロケーター
  • 「クラス」 - 属性
  • 「sortButtonActive」 - 待っている「クラス」の値

セレン V3.01

于 2017-01-20T15:55:07.637 に答える
0

単純な do-while ループ System.currentTimeMillis(); を使用して実行することもできます。メソッドを使用して無限ループを回避できます

long startTime = System.currentTimeMillis();
String disabled;
do{
   disabled = element.getAttribute("attributeName").trim();
}while(disabled!="false" && System.currentTimeMillis()-startTime<10000);
于 2017-08-01T05:12:38.457 に答える