TestProject Forum
Powered by leading experts in the test automation community

Not able to clear field

Hey Guys ,

Hope everyone is fine during this pandemic ,

Currently i’m facing one issue in clearing field in Selenium ,I have tried almost every possible thing but not able to do it :

My Scenario is :
I need to open loan documents ,i need to clear some wrongly pre-populated masked dob field [I cannot continue with this because while trying to finish signing doc, it throws error at this field saying incorrect date format .So i need to clear the field and enter dob in MM/DD/YYYY format .

Things which i tried :

  1. Using javaScript Executor je.executeScript(“arguments[0].value=’ '”,element)
  2. Using sendKeys(Keys.CONTROL + “a”) then sendKeys(Keys.BACKSPACE) or sendKeys(Keys.DELETE)
  3. Of course i tried using clear method as well

My xpath is something like below :
I’m operating on input field Only but not able to clear the value
//label[text()=“Required - Applicant Birthdate”]/parent::div/input[starts-with(@id,‘masked-tab’)]

Much appreciated if anyone can help me in solving this issue .

Thanks.

Hello @karthikprathyu143
Can you please check the type of the element you are trying to clear?
You can use either chrome developer tools or the Element explorer in the recorder.
Make sure oyur locator is selecting the right element while trying to clear the field.
For example you said you do sendKeys(Keys.CONTROL + a), when you do that does it select all the contents of the field?

Yes it selects always but won’t delete it , I use something like below :
driver.findElement(LoanDocSignPageLocators.DOB_FIELD).sendKeys(Keys.CONTROL+“a”);
driver.findElement(LoanDocSignPageLocators.DOB_FIELD).sendKeys(Keys.DELETE);
In the image DOB field highlighted ,that field i wanna clear ,


But it won’t delete it ,it just do select but it won’t clear not sure why it won’t delete after selecting ,
Thanks.

Can you try this

driver.findElement(LoanDocSignPageLocators.DOB_FIELD).sendKeys(Keys.chord(Keys.CONTROL,"a",Keys.DELETE));

Thanks ran that really helps and it solves 50% of my problem, it worked but with minor change .
driver.findElement(LoanDocSignPageLocators.DOB_FIELD).sendKeys(Keys.chord(Keys.CONTROL, “a”), Keys.DELETE));

Great, happy that worked.

Clear field issue solved but later i was not able to type into that field ,it nearly took some 4 hours for me to debug that ,finally i was able to type into that field . I have used javascript executor also but didn’t worked .Finally i have used below one which solved my issue i.e

        wait.until(ExpectedConditions.visibilityOfElementLocated(LoanDocSignPageLocators.DOB_FIELD)).sendKeys("01/09/1996");

The above one also works only when :
we need to switch the control to different field then coming back to input field which we cleared .

Also after typing text using the statement mentioned ,control was not switching to other buttons ,So i have used exception handling and solved my problem.

Hope it helps others in future if they face this issue.