2016-04-13 5 views
1

JavaScriptの警告が表示されるまでボタンをクリックします。ここでSeleniumに警告が表示されるまでボタンをクリックしてください

は私がやりたいものです。

while(!ExpectedConditions.alertIsPresent()) 
    button.click(); 

しかし、式がブール条件に評価されないので、これは動作しません。

私が試してみました:

while(ExpectedConditions.alertIsPresent() == null) 
    button.click(); 
をしかし、これはループに入ることはありませんになります。任意の指導

答えて

0

述語ExpectedConditions.alertIsPresentwhileに直接評価することはできません。 あなたはウェイターでそれを使用することができます。

WebDriverWait wait = new WebDriverWait(driver, 20); 
wait.until(ExpectedConditions.alertIsPresent()); 

をしかし、あなたの場合にはより良い選択は、アラートが存在するまで、ボタンをクリックする述語を実装するために、次のようになります。

WebElement button = driver.findElement(By.id("...")); 

// clicks on the button every 100ms until the alert is present 
Alert alert = new WebDriverWait(driver, 20, 100).until((WebDriver drv)->{ 
    try{ 
     button.click(); 
     return drv.switchTo().alert(); 
    }catch(NoAlertPresentException ex){ 
     return null; 
    } 
}); 

// accept the alert 
alert.accept(); 
0

あなたはこの回避策で行くことができてくれてありがとう: -

while(alert.getClass().getCanonicalName().toString().equals("org.openqa.selenium.remote.RemoteWebDriver.RemoteAlert")){ 
button.click(); 
} 
関連する問題