Search code examples
rubywatir

how to call clear method on the element object


In my project I have located the text_Field via some other element(via label), something like

element.following_sibling(tag_name: 'input').send_keys 'something'

But now it's typing into the text_field without any problem but I am missing clear method of text_field as it combines both clear and type together. Now I have to call clear method on element.following_sibling(tag_name: 'input') but since it's returning element object, I couldn't do that. Is there any way I can call clear method here? Or can I pass this object by some way to the text_field() method? Any help appreciated.

I know it can be called by converting the watir element into selenium element as given below

element.following_sibling(tag_name: 'input').wd.clear

But here I am missing WATIR waiting time for an element.


Solution

  • This appears to be a limitation in Watir's adjacent methods:

      klass = if !plural && opt[:tag_name]
                Watir.element_class_for(opt[:tag_name])
              elsif !plural
                HTMLElement
              elsif opt[:tag_name]
                Object.const_get("#{Watir.element_class_for(opt[:tag_name])}Collection")
              else
                HTMLElementCollection
              end
    

    Notice that only the :tag_name is used for determining the element's class. For input elements, we need to also consider the type attribute so that we can the right sub-class.

    We should fix this in Watir (logged as Issue 878), but in the mean time, you can manually correct the class (ie Watir::TextField) using #to_subtype:

    element.following_sibling(tag_name: 'input').to_subtype.clear