In webdriver, how to ask to webdriver to wait until text is present in text field.
actually i have one kendo text field whose values comes from database which takes
A one liner that works and use lambda function.
wait.until((ExpectedCondition<Boolean>) driver -> driver.findElement(By.id("elementId")).getAttribute("value").length() != 0);
Using WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)
and ExpectedCondition (org.openqa.selenium.support.ui.ExpectedConditions)
objects
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("element_id"), "The Text"));
This is my solution for sending text to input:
public void sendKeysToElement(WebDriver driver, WebElement webElement, String text) {
WebDriverWait wait = new WebDriverWait(driver, Configuration.standardWaitTime);
try {
wait.until(ExpectedConditions.and(
ExpectedConditions.not(ExpectedConditions.attributeToBeNotEmpty(webElement, "value")),
ExpectedConditions.elementToBeClickable(webElement)));
webElement.sendKeys(text);
wait.until(ExpectedConditions.textToBePresentInElementValue(webElement, text));
activeElementFocusChange(driver);
} catch (Exception e) {
Configuration.printStackTraceException(e);
}
}
WebElement nameInput = driver.findElement(By.id("name"));
sendKeysToElement(driver, nameInput, "some text");
You can use WebDriverWait. From docs example:
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(...).getText().length() != 0;
}
});
You can use a simple method in which you need to pass driver object webelement in which text is going to come and the text which is goint to come.
public static void waitForTextToAppear(WebDriver newDriver, String textToAppear, WebElement element) {
WebDriverWait wait = new WebDriverWait(newDriver,30);
wait.until(ExpectedConditions.textToBePresentInElement(element, textToAppear));
}
You can use WebDriverWait
. From docs example:
above ans using .getTex()
this is not returning text from input field
use .getAttribute("value")
instead of getText()
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(...).getAttribute("value").length() != 0;
}
});
tested 100% working hope this will help