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

It’s pretty positive that you should use .clear or maybe not as if it doesn’t work for me, maybe I’m just implementing it the wrong way. I'm not sure.

Example:

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

Can someone tell me how to simply clear the field and then type in a new line?

+4
source share
3 answers

Assuming we are talking about a text field (i.e. you are not trying to clear / enter the div tag), the .set() and .value= methods automatically delete the text field before entering the value.

So one of the following will work:

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

Note that it is generally recommended to use .set , since .value= does not fire events.

+9
source

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

The element was Watir :: Input:

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

after clearing the field, I managed to enter the text.

 browser.input(:id => "formLib1").send_keys "hi" 
+3
source

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

The element was Watir :: HTMLElement:

 [2] pry(#<Object>)> field.class => Watir::HTMLElement field.methods.grep /^(set|clear)$/ => [] 

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

 count = 0 while field.value != "" && count < 50 field.send_keys(:backspace) count += 1 end field.send_keys "hi" 
+1
source

Source: https://habr.com/ru/post/1434005/


All Articles