2016-11-30 5 views
1

ドロップダウンリストをクリックすると、リスト内の要素をクリックできません。Webdriver TEXTが表示されるのを待つ

メソッド作成したが動作しないものがあります。

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{ 
    WebDriverWait wait = new WebDriverWait(driver, 10); 

    wait.until(ExpectedConditions.visibilityOf(element)); 
    WebElement locator = element; 
    locator.click(); 

    WebElement textToClick = driver.findElement(By.linkText(textToAppear)); 
    wait.until(ExpectedConditions.elementToBeClickable(textToClick)); 
    textToClick.click(); 
} 

thread.sleep使用は、作品には思われるが、私は誰もが待って、私は主ボタンをクリックすると、特定のテキスト要素をクリックする方法をお勧めすることができ、この方法を使用したくありませんか?

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{ 
    WebDriverWait wait = new WebDriverWait(driver, 10); 

    wait.until(ExpectedConditions.visibilityOf(element)); 
    WebElement locator = element; 
    locator.click(); 

    Thread.sleep(3000); 
    driver.findElement(By.linkText(textToAppear)).click();; 

} 

バーベキューソースに

enter image description here

をクリックする必要がときthread.sleep()が成功したおかげであなたの助け

+0

あなたはまた私達の参照のための例外トレースを追加することができますか? –

+1

-\t textToBePresentInElementまたは\t textToBePresentInElementValueのような既存のExpectedConditionsを使用します。 API - \t textToBePresentInElementを確認してください。 – Grasshopper

+0

関連するHTMLを投稿してください。 – JeffC

答えて

0

のための独自のを使用して、私はバーベキューソースをクリックする必要がありますのでご注意くださいあなたのクリック後にテキストが存在するまで待つためにFluentWaitを実装する:

以下の方法は、トリック行わのように見えるあなたの助けの
Wait wait = new FluentWait<>(this.driver) 
    .withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS) 
    .pollingEvery(500, TimeUnit.MILLISECONDS) 
    .ignoring(StaleElementReferenceException.class) 
    .ignoring(NoSuchElementException.class) 
    .ignoring(ElementNotVisibleException.class); 

WebElement foo = wait.until(new Function() { 
    public WebElement apply(WebDriver driver) { 
    return element.getText().length() > 0; 
    } 
}); 
0

すべての感謝:

public static void waitForTextToAppearAndClick(WebDriver driver, WebElement element, String textToAppear) throws InterruptedException{ 
    WebDriverWait wait = new WebDriverWait(driver, 10); 

    wait.until(ExpectedConditions.visibilityOf(element)); 
    WebElement locator = element; 
    locator.click(); 

    wait.until(ExpectedConditions.elementToBeClickable(By.linkText(textToAppear))); 
    driver.findElement(By.linkText(textToAppear)).click(); 
} 
関連する問題