Clear input field and enter new info using Watir? (Ruby, Watir)

samayres1992 picture samayres1992 · Sep 13, 2012 · Viewed 15.6k times · Source

Pretty positive you have to use .clear, or maybe not as it doesn't seem to be working for me, maybe i'm just implementing it wrong I'm unsure.

Example:

browser.div(:id => "formLib1").clear.type("input", "hi")

Can anyone tell me how to simply clear a field then enter in a new string?

Answer

Justin Ko picture Justin Ko · Sep 13, 2012

Assuming we are talking about a text field (ie you are not trying to clear/input a div tag), the .set() and .value= methods automatically clear the text field before inputting the value.

So one of the following would work:

browser.text_field(:id, 'yourid').set('hi')
browser.text_field(:id, 'yourid').value = 'hi'

Note that it is usually preferred to use .set since .value= does not fire events.