Hardik Bhatt Hardik Bhatt - 2 months ago 15
Ruby Question

In Watir-Webdriver I need to automation code for Datepicker

Image of 2 datepicker:


I have 2 datepicker as shown in the screenshot I need to Automate in Watir-Webdriver.

They have same class in it so I am not able to select the 2 datepicker.

The code which I have written is:

("browser.img(:class => "ui-datepicker-trigger").click")

through this I am able to click the 1 date picker but how can I select the 2 date picker?


At a minimum, you can use the :index locator to select the second date picker. The :index locator specifies which occurrence of the matching elements to use. For example, index: 0 selects the first match (and is the default). index: 1 selects the second match, etc.

To pick the second date picker would be:

browser.img(:class => "ui-datepicker-trigger", :index => 1).click

The downside of :index locators is that they will unexpectedly break when other similar elements are added. For example, adding another date picker before the instruction date will cause the above code to update the wrong date picker. A better approach would be to find the date picker that is specific to the delivery due date.

For example, the HTML might be like:

    Instruction Date:
    <input type="text" id="instruction_date">
    <img class="ui-datepicker-trigger">
    Delivery Due Date:
    <input type="text" id="delivery_due_date">
    <img class="ui-datepicker-trigger">

In this markup, an alternative to looking for the second date picker, is to look for the date picker in the same li as the delivery due date field. Taking this approach may be more robust to future changes:

browser.text_field(id: 'delivery_due_date').parent.img(class: 'ui-datepicker-trigger').click