Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

watir

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?

like image 660
samayres1992 Avatar asked Sep 13 '12 13:09

samayres1992


3 Answers

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.

like image 125
Justin Ko Avatar answered Nov 09 '22 13:11

Justin Ko


I had a similar issue, and, for some reason, .set() and .value= were not available/working for the element.

The element was a Watir::Input:

browser.input(:id => "formLib1").to_subtype.clear

after clearing the field I was able to enter text.

browser.input(:id => "formLib1").send_keys "hi"
like image 20
satoukum Avatar answered Nov 09 '22 13:11

satoukum


I had a similar issue, and, for some reason, .set() and .value= were not available for the element.

The element was a Watir::HTMLElement:

[2] pry(#<Object>)> field.class
=> Watir::HTMLElement

field.methods.grep /^(set|clear)$/
=> []

I resorted to sending the backspace key until the value of the field was "":

count = 0
while field.value != "" && count < 50
  field.send_keys(:backspace)
  count += 1
end
field.send_keys "hi"
like image 24
cartoloupe Avatar answered Nov 09 '22 14:11

cartoloupe