Selenium: How do I use javascript to clear a value from a form field?

Andrew picture Andrew · Jan 9, 2009 · Viewed 8.9k times · Source

I'm using the selenium IDE and the Selenium-Fitnesse Bridge fixture and I'm trying to test that when I clear a default value out of a form field, my form displays an error message.

So when I record with the Selenium IDE, what it does is the equivalent of telling Selenium to type nothing.

| type | text_field |  |

The problem with this is that the Fitnesse fixture I'm using expects that second argument to not be null.

Is there a way in Selenium to "clear a value" rather than "typing nothing"?

Answer

Gavin Miller picture Gavin Miller · Jan 9, 2009

You can do it via javascript as such:

| verifyEval | javascript{this.browserbot.getCurrentWindow().document.getElementById('CONTROL_ID').value = ''} || 

Effectively the verifyEval statement allows you to execute any piece of javascript that you'd like. Makes some difficult problems to accomplish with Selenium much simpler.

I used this tutorial (today believe it or not) to figure things out.