Home > Blockchain >  How do I wait for a value in a text box with Selenium in C#
How do I wait for a value in a text box with Selenium in C#

Time:11-10

I have a text input that is changed by Javascript based on values of other inputs.

How do I get Selenium to wait for it to change?

I currently have tried both

var wait3 = new WebDriverWait(_driver, TimeSpan.FromSeconds(5)).Until(driver => !driver.FindElement(By.Id("gbpAmount")).Equals(""));

and

bool pop= wait.Until<bool>((d) =>
            {
                var populated1 = d.FindElement(By.Id("gbpAmount")).Text != "";
                var populated2 = d.FindElement(By.Id("gbpAmount")).Text != "0";
                if (populated1 && populated2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });

However neither of these methods work. Either they just pass through and when I fetch and check the element afterwards it is "" or they just exceed the wait time.

Yet clearly on the screen the value is changed...

CodePudding user response:

In Java the approach would be:

(new WebDriverWait(driver, Duration.ofSeconds(5))).until( ExpectedConditions.not(ExpectedConditions.attributeToBe(By.id("gbpAmount"), "value", "")));
String strAmount = driver.findElement(By.id("gbpAmount")).getAttribute("value");

So the equivalent in C# should work.

CodePudding user response:

You need to check the value DOM property of text inputs, not the Value property of IWebElement.

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));

wait.Until(d => d.FindElement(By.Id("gbpAmount")).GetProperty("value") != ""
             && d.FindElement(By.Id("gbpAmount")).GetProperty("value") != "0");

This should work for both <input type="X"> and <textarea>.

If the values are numeric in nature, this should wait until there is a non-zero result:

wait.Until(d => decimal.TryParse(d.FindElement(By.Id("gbpAmount")).GetProperty("value"), out decimal result) ? result != 0 : false);

Note that negative numbers will cause this to return true. If you want a positive number, change result != 0 to result > 0.

  • Related