简体   繁体   中英

Clear input field and enter new info using Watir? (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?

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.

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"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM