Isaac
Isaac

Reputation: 9682

Clear text from textarea with selenium

I've got some tests where I'm checking that the proper error message appears when text in certain fields are invalid. One check for validity is that a certain textarea element is not empty.

If this textarea already has text in it, how can I tell selenium to clear the field?

something like:

driver.get_element_by_id('foo').clear_field()

Upvotes: 212

Views: 405859

Answers (18)

MP23
MP23

Reputation: 1773

Forcing input value to an empty string also works with the help of execute_script function

element = driver.find_element(By.ID,"some_element")
driver.execute_script('arguments[0].value = "0";', element)

Upvotes: 0

Rajeev Kumar
Rajeev Kumar

Reputation: 9

Try using this

text=((Keys.BACKSPACE*20)+(Keys.DELETE * 20))

Upvotes: 0

Samiya Khan
Samiya Khan

Reputation: 369

First find the element

element = driver.find_element_by_id('your element id')

Clear text in Input or Textarea: If the element is input or textarea, you can directly remove the using clear() function.

element.clear()

Reset Radio button or Checkbox: Just click again the radio button or checkbox item.

element.click()

Deselect the Dropdow: In select, you can deselect using following ways.

Select select = new Select(element);

select.deselectByVisibleText("Female");
select.deselectByValue("1");
select.deselectByIndex(1);
select.deselectAll();

Upvotes: 1

UFO
UFO

Reputation: 161

I recommend:

WebElement.send_keys([Keys.BACKSPACE] * 1000)

This is much faster and more elegant than a while loop. And Keys.BACKSPACE works on Mac. These all didn't work for me on Chrome on Mac:

WebElement.clear()
WebElement.send_keys(Keys.CONTROL, 'a')
WebElement.send_keys(Keys.DELETE)
while _ in range(1000):
    WebElement.send_keys(Keys.DELETE)

Upvotes: 6

Bagghi Daku
Bagghi Daku

Reputation: 652

If clear() is not working, you can simulate the press of Backspace multiple times using: driver.get_element_by_id('foo').send_keys(Keys.BACKSPACE)

To execute this multiple times, use:

from selenium.webdriver.common.keys import Keys

# Replace 100 with the number of time you want to press backspace.
for i in range(0, 100): 
    driver.get_element_by_id('foo').send_keys(Keys.BACKSPACE)

Upvotes: 1

Abubakar Ijaz
Abubakar Ijaz

Reputation: 51

Try:

driver=self.driver
driver.find_element_by_xpath('Path').send_keys(Keys.CONTROL + "a")      
driver.find_element_by_xpath('Path').send_keys(Keys.BACK_SPACE)

Upvotes: 2

Kevin Campbell
Kevin Campbell

Reputation: 774

The CTRL+A send_keys solution did not work for me in react. Testing in the browser directly, CTRL+A just sends the cursor to the start of the text element. Instead, this seems to be reliable for selecting all content in the element.

preferred_name_field.send_keys(Keys.SHIFT, Keys.ARROW_UP)
preferred_name_field.send_keys(Keys.DELETE)

Upvotes: 9

Natan Lieberman
Natan Lieberman

Reputation: 11

from selenium.webdriver.common.keys import Keys

element = driver.find_element_by_css_selector('foo') element.send_keys(Keys.CONTROL + Keys.BACKSPACE)

Upvotes: 1

gitaoh
gitaoh

Reputation: 351

Am using selenium==3.141.0 and I don't know why

WebElement.clear()

is not working.

I used

WebElement.send_keys(Keys.CONTROL, 'a')
WebElement.send_keys(Keys.DELETE)

Which perfectly worked for me.

Upvotes: 9

Fenix
Fenix

Reputation: 2401

Option a)

If you want to ensure keyboard events are fired, consider using sendKeys(CharSequence).

Example 1:

 from selenium.webdriver.common.keys import Keys
 # ...
 webElement.sendKeys(Keys.CONTROL + "a")
 webElement.sendKeys(Keys.DELETE)

Example 2:

 from selenium.webdriver.common.keys import Keys
 # ...
 webElement.sendKeys(Keys.BACK_SPACE)  //do repeatedly, e.g. in while loop

WebElement

There are many ways to get the required WebElement, e.g.:

  • driver.find_element_by_id
  • driver.find_element_by_xpath
  • driver.find_element

Option b)

 webElement.clear()

If this element is a text entry element, this will clear the value.

Note that the events fired by this event may not be as you'd expect. In particular, we don't fire any keyboard or mouse events.

Upvotes: 148

Jortega
Jortega

Reputation: 3790

I ran into a field where .clear() did not work. Using a combination of the first two answers worked for this field.

from selenium.webdriver.common.keys import Keys

#...your code (I was using python 3)

driver.find_element_by_id('foo').send_keys(Keys.CONTROL + "a")
driver.find_element_by_id('foo').send_keys(Keys.DELETE)

Upvotes: 32

m0h17
m0h17

Reputation: 367

In my experience, this turned out to be the most efficient

driver.find_element_by_css_selector('foo').send_keys(u'\ue009' + u'\ue003')

We are sending Ctrl + Backspace to delete all characters from the input, you can also replace backspace with delete.

EDIT: removed Keys dependency

Upvotes: 15

Victoria
Victoria

Reputation: 299

driver.find_element_by_xpath("path").send_keys(Keys.CONTROL + u'\ue003') worked great with FireFox

  • u'\ue003' is a BACK_SPACE for those like me - never remembering it)

Upvotes: 1

ShadowGames
ShadowGames

Reputation: 1337

With a simple call of clear() it appears in the DOM that the corresponding input/textarea component still has its old value, so any following changes on that component (e.g. filling the component with a new value) will not be processed in time.

If you take a look in the selenium source code you'll find that the clear()-method is documented with the following comment:

/** If this element is a text entry element, this will clear the value. Has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements. Note that the events fired by this event may not be as you'd expect. In particular, we don't fire any keyboard or mouse events. If you want to ensure keyboard events are fired, consider using something like {@link #sendKeys(CharSequence...)} with the backspace key. To ensure you get a change event, consider following with a call to {@link #sendKeys(CharSequence...)} with the tab key. */

So using this helpful hint to clear an input/textarea (component that already has a value) AND assign a new value to it, you'll get some code like the following:

public void waitAndClearFollowedByKeys(By by, CharSequence keys) {
    LOG.debug("clearing element");
    wait(by, true).clear();
    sendKeys(by, Keys.BACK_SPACE.toString() + keys);
}

public void sendKeys(By by, CharSequence keysToSend) {
    WebElement webElement = wait(by, true);
    LOG.info("sending keys '{}' to {}", escapeProperly(keysToSend), by);
    webElement.sendKeys(keysToSend);
    LOG.info("keys sent");
}

private String escapeProperly(CharSequence keysToSend) {
    String result = "" + keysToSend;
    result = result.replace(Keys.TAB, "\\t");
    result = result.replace(Keys.ENTER, "\\n");
    result = result.replace(Keys.RETURN, "\\r");

    return result;
}

Sorry for this code being Java and not Python. Also, I had to skip out an additional "waitUntilPageIsReady()-method that would make this post way too long.

Hope this helps you on your journey with Selenium!

Upvotes: 3

vaibhavcool20
vaibhavcool20

Reputation: 871

for java

driver.findelement(By.id('foo').clear();

or

webElement.clear();

If this element is a text entry element, this will clear the value.

Upvotes: 6

iamsankalp89
iamsankalp89

Reputation: 4749

It is general syntax

driver.find_element_by_id('Locator value').clear();
driver.find_element_by_name('Locator value').clear();

Upvotes: 6

Isaac
Isaac

Reputation: 9682

driver.find_element_by_id('foo').clear()

Upvotes: 306

Union find
Union find

Reputation: 8160

In the most recent Selenium version, use:

driver.find_element_by_id('foo').clear()

Upvotes: 20

Related Questions