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...
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()
17 Answers
driver.find_element_by_id('foo').clear()
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