Wait before sendkeys in Selenium Java

3.3k Views Asked by At

I'm using Java, Selenium, and chrome for test automation. I want to enter a text, wait for it to be displayed and then click on TAB, I want to avoid thread.sleep so i'm using this code :

        WebElement societe = wait.until(ExpectedConditions.elementToBeClickable(By.id("AutoComplInputBoxfld_XSociete")));
        societe.sendKeys("Text");
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        societe.sendKeys(Keys.TAB);

But it leave the field blank and goes to the next element.

  1. How can i fix it?
  2. Is there a way to use explicit wait before sendkeys?
3

There are 3 best solutions below

0
On

You can use something like this:

WebElement societe = driver.findElement(By.tagName("input"));
Wait<WebElement> wait = new FluentWait<>(societe);
societe.sendKeys("Text");;
wait.until(webElement -> {
    String value = webElement.getAttribute("value");
    return "Text".equals(value);
});

you need to configure fluent wait to have certain timeouts, etc.

0
On

I would do this:

WebElement societe = wait.until(ExpectedConditions.elementToBeClickable(By.id("AutoComplInputBoxfld_XSociete")));
        societe.sendKeys("Text");
        while(!societe.getAttribute("value").equals("Text")) {//wait}
 societe.sendKeys(Keys.TAB);
0
On

Try this

wait.until(ExpectedConditions.textToBePresentInElement(element,"Textexpected))

Reference

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#textToBePresentInElement-org.openqa.selenium.WebElement-java.lang.String-

Or

You can try

wait.until(ExpectedConditions.textToBe(element,"Textexpected))

Ps-ignore formatting using device