watir

How can I write this xpath expression in WATIR methods


I have to form the following xpath via watir methods,

//span[@class='fieldTitle-small required z-label'][contains(.,'Address Type')]/../following-sibling::div//input

Is there a way to call the watir methods one after another to form this xpath?


Solution

  • That XPath could be done using Watir methods as:

    browser.span(class: %w(fieldTitle-small required z-label), text: /Address Type/) # //span[@class='fieldTitle-small required z-label'][contains(.,'Address Type')]
           .parent                                                                   # /..
           .following_sibling(tag_name: 'div')                                       # /following-sibling::div                  
           .input                                                                    # //input                         
    

    Note that when using a :class locator, an Array can be used to specify multiple classes that can appear in any order. The element only has to include those classes, but it can also include any others. For example, browser.span(class: %w(fieldTitle-small z-label), text: /Address Type/), which has the "required" class removed, would match <span class="fieldTitle-small required z-label"> and <span class="fieldTitle-small z-label">.