I ran into a scenario today where clear() wasn't actually clearing the input element and I'm hoping someone can shed some light on this as I've never ran into this before.
It looks like the method executes successfully without throwing an exception but it just doesn't clear.
Clear Method - this is in a separate framework that is being used by the calling project
public void Clear(By element)
{
try
{
driver.FindElement(element).Clear();
}
catch (Exception ex)
{
DTAF.Helpers.Screenshot.TakeScreenshot(driver);
throw ex;
}
}
Element HTML - you can see it has value that I was trying to clear
<input aria-invalid="false" id="txtCustomerEditFirstName" type="text" data-testid="firstname" class="MuiFilledInput-input MuiInputBase-input css-1ncak0i" value="test123">
I was able to execute just raw selenium in the Immediate window and it looked like it cleared the input but when I clicked the actual element in the UI the value just came back so it didn't actually clear it.
I did find a workaround where I just get the input value attribute and loop through that pressing the Backspace key until theres nothing left. It works just fine but I'd prefer to use the selenium clear method if possible.
EDIT
Element Locator
public static By FirstNameTextbox = By.Id("txtCustomerEditFirstName");
Calling method
public EditUnregisteredCustomerProfileModal ClearAndEnterFirstName(string firstName)
{
try
{
ElementActions.Clear(EditUnregisteredCustomerProfileModalElements.FirstNameTextbox);
ElementActions.Type(EditUnregisteredCustomerProfileModalElements.FirstNameTextbox, firstName);
}
catch (Exception e)
{
DTAFLogger.GetLogger().Error($"Failed to clear and enter unregistered customer first name '{firstName}'. {e.Message}");
Assert.Fail($"Failed to clear and enter unregistered customer first name '{firstName}'. {e.Message}");
}
return this;
}