2013-02-12 7 views
6

wait.until(ExpectedConditions)に2つの要素を使用します。 私はテストを実行しており、Element1またはElement2のいずれかが表示されるまで待つためには、WebDriverが必要です。それから、私は最初に登場する人を選ぶ必要があります。私が試した:wait.until(ExpectedConditions.visibilityOf Element1 OR Element2)

WebDriverWait wait = new WebDriverWait(driver, 60); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[@class='....']"))) || wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='... ']"))); 

     // Then I would need: 

String result = driver.findElement(By.xpath("...")).getText() || driver.findElement(By.xpath("...")).getText(); 

を要約すると、私は2つの要素のいずれかが表示されるまで待つ必要があります。表示されている人を選んでください(同時に表示されません) 助けてください。

+2

掻き回し、私たちは:-) –

+0

Tak tochno !!!!! –

+0

попробуй[Watir](http://watirwebdriver.com/)^ _^ –

答えて

1

残念ながら、このようなコマンドはありません。あなたは試してキャッチすることでこれを克服することができます、またはWatijを使用することをお勧めします。

+0

ありがとうございます、私は試してみます –

7

これはヘルパークラスで宣言したメソッドです。チャームのように機能します。ちょうどあなた自身のExpectedConditionを作成し、それがロケータで見つかった要素のいずれかを返すます

public static ExpectedCondition<WebElement> oneOfElementsLocatedVisible(By... args) 
{ 
    final List<By> byes = Arrays.asList(args); 
    return new ExpectedCondition<WebElement>() 
    { 
     @Override 
     public Boolean apply(WebDriver driver) 
     { 
      for (By by : byes) 
      { 
       WebElement el; 
       try {el = driver.findElement(by);} catch (Exception r) {continue;} 
       if (el.isDisplayed()) return el; 
      } 
      return false; 
     } 
    }; 
} 

をそしてあなたはそれをこのように使用することができます。

Wait wait = new WebDriverWait(driver, Timeouts.WAIT_FOR_PAGE_TO_LOAD_TIMEOUT); 
WebElement webElement = (WebElement) wait.until(
     Helper.oneOfElementsLocatedVisible(
       By.xpath(SERVICE_TITLE_LOCATOR), 
       By.xpath(ATTENTION_REQUEST_ALREADY_PRESENTS_WINDOW_LOCATOR) 
     ) 
); 
ここ

SERVICE_TITLE_LOCATORATTENTION_REQUEST_ALREADY_PRESENTS_WINDOW_LOCATORはのための2つの静的ロケータですページ。

2

xpathに "OR"を入れると、問題は簡単な解決策だと思います。

WebDriverWait wait = new WebDriverWait(driver, 60); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[@class='....'] | //h3[@class='... ']"))); 

そして、例えば結果の使用を印刷します:

if(driver.findElements(By.xpath("//h2[@class='....']")).size()>0){ 
     driver.findElement(By.xpath("//h2[@class='....']")).getText(); 
}else{ 
     driver.findElement(By.xpath("//h3[@class='....']")).getText(); 
} 
0

あり待たする別の方法があるが、それは予想される条件を使用して、イマイチ、それは代わりに、ラムダ式を使用しています。..

wait.Until(x => driver.FindElements(By.Xpath("//h3[@class='... ']")).Count > 0 || driver.FindElements(By.Xpath("//h2[@class='... ']")).Count > 0); 
0

このようにCSSSelectorを使用することもできます。

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h2.someClass, h3.otherClass"))); 

someClassとotherClassを、xpathの[...]にあったものに置き換えます。

11

これには固有の解決策があります。orメソッドcheck the docです。

あなたはそうのようにそれを使用する:この前

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='FirstElement' or @id='SecondElement']"))); 

私はwait.until(ExpectedConditions.or(...を使用しようとしていた、互換性がありませんでした:

driverWait.until(ExpectedConditions.or(
    ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.something")), 
    ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.anything")))); 
+1

どのような要素が条件に当てはまるかをどうすれば保証できますか? – Mak13

-1

は、明示的な待ちを使用して、このための簡単な解決策があります2.53.0より前のセレンのバージョン。

関連する問題