Reputation: 139
I have performed an action on WebDriver
(Say, I clicked a button), resultant is a text will display on page.
We do not know locator element for the text, but we do know what text will display.
Please suggest a way to wait for the text to display.
I have come across WebDriverWait
, but it requires WebElement
to wait for text.
Upvotes: 3
Views: 11518
Reputation: 1868
For waiting text to be displayed in element:
private ExpectedCondition elementTextDisplayed(WebElement element, String text) {
return new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return element.getText().equals(text);
}
};
}
protected void waitForElementTextDisplayed(WebElement element, String text) {
wait.until(elementTextDisplayed(element, text));
}
or
public void waitUntilTextToBePresentInElement(WebElement element, String text){
wait.until(ExpectedConditions.textToBePresentInElement(element, text));
}
Upvotes: 0
Reputation: 48
WebDriverWait can be used even if you don't know the exact element. If the expected text only has 1 occurrence on the page, you can reach it through an Xpath like this:
WebDriverWait wait = new WebDriverWait(driver, numberOfSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'my text')]")));
Upvotes: 1
Reputation: 16201
Go for xpath text based search. It allows you to find an element based on the text
// with * we are doing tag indepenedent search. If you know the tag, say it's a `div`, then //div[contains(text(),'Text To find')] can be done
By byXpath = By.xpath("//*[contains(text(),'Text To find')]");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(byXpath));
Upvotes: 11