How to clear the text from textbox or text area using Selenium

There is a text box on a webpage. My aim is to fill it with some values, then clear it, and then fill it with some other value.

I can do the fill part with sendKeys() method. But how to clear the text from the textbox?

PS: I am using Selenium Python

1 Like

Hi Rebecca,

There are many ways by which you can clear the text in a text box (partially or fully), such as:

  1. driver.get_element_by_id('foo').clear()
  2. driver.find_element_by_id('foo').clear()
  3. webElement.clear()
driver.find_element_by_id('foo').send_keys(Keys.CONTROL + "a");
  1. For Partial:

webElement.sendKeys(Keys.BACK_SPACE) # do repeatedly, e.g. in while loop

  1. driver.find_element_by_css_selector('foo').send_keys(u'\ue009' + u'\ue003')
  • u’\ue009’ is a CONTROL
  • u’\ue003’ is a BACK_SPACE

Hope this helps