Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium clear() command doesn't clear the element

I have been writing selenium scripts for a while in Java. I encountered a very weird issue today. Here is the issue:

I cleared a text field using webelement.clear() method, later while executing next command (click event), the text area I had previously cleared, is now populated with previously filled value.

Here is the code snippet:

mobileNumField.get(0).clear();
Thread.sleep(4500);
emailAddress.get(0).click();
emailAddress.get(0).clear();
Thread.sleep(4500);
emailAddress.get(0).sendKeys(Keys.TAB);
like image 391
karthikeya acharya Avatar asked Jun 04 '18 09:06

karthikeya acharya


People also ask

What is clear command in Selenium?

clear( ) predefined method of Selenium 'WebDriver' Class is used to clear the text entered or displayed in the text fields. Now lets clear the text inside the Text Area field using the clear( ) predefined method of Selenium 'WebDriver' class.

How we can clear the cookie in Selenium?

Navigate to the Chrome settings page with Selenium by executing the driver. get('chrome://settings/clearBrowserData') . Click on the Clear Data button to clear the cache.

How do you delete text in Selenium?

As shown clearly in the script for this example, you simply need to use element. clear() where element is the text input.

Which method returns an empty list on zero match of element?

The findElements command returns an empty list if there are no elements found using the given locator strategy and locator value. Below is the syntax of findElements command.


1 Answers

I don't know the exact reason for your element keeping its value, but you can try an alternative text clearance by sending 'Ctrl+A+Delete' key combination using sendKeys method of the element's object:

emailAddress.sendKeys(Keys.chord(Keys.CONTROL,"a", Keys.DELETE));
like image 69
AutomatedOwl Avatar answered Oct 22 '22 09:10

AutomatedOwl