Rajagopalan
Rajagopalan

Reputation: 6064

Write WATIR equivalent Code for the given xpath

I am trying to write the watir code for the given xpath, but it's clicking the wrong button, the given below xpath works perfectly.

@browser.element(xpath: "//div[text()='#{locator.strip}']/../following-sibling::div/input/following-sibling::div").click

WATIR Code

@browser.div(text: locator.strip).parent.following_sibling(tag_name: 'div').input.following_sibling(tag_name: 'div').click

I am trying to click the select list button to open the option, but it wrongly clicks the select list which is present below my targetted select_list. Can anyone help me where I go wrong? If you see the below pic, It has to open the title, but it's opening the ID type.

enter image description here

Update

<div class="v-csslayout v-layout v-widget spml-cell v-csslayout-spml-cell spml-property v-csslayout-spml-property span4 v-csslayout-span4">
  <div class="v-caption v-caption-spml-value v-caption-uppercase v-caption-editable" id="gwt-uid-43" for="gwt-uid-44">
    <div class="v-captiontext">Title</div>
  </div>
  <div role="combobox" class="v-filterselect v-widget spml-value v-filterselect-spml-value uppercase v-filterselect-uppercase editable v-filterselect-editable v-filterselect-prompt" id="ClientDetails/Title">
    <input type="text" class="v-filterselect-input" autocomplete="off" id="gwt-uid-44" aria-labelledby="gwt-uid-43" tabindex="0" dir="" style="width: 281px;">
    <div class="v-filterselect-button" aria-hidden="true" role="button"></div>
  </div>
</div>

Upvotes: 1

Views: 301

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

The difference between the XPath and Watir code is how they interpret the "text" of an element:

  • In the XPath, //div[text()='#{locator.strip}'] says that the very first text node of the div must equal the locator.
  • In Watir , div(text: locator.strip) says that the concatenation of all text nodes of the div must equal the locator.

As a result, you end up starting from different elements:

  • XPath starts from <div class="v-captiontext">Title</div>
  • Watir starts from <div class="v-csslayout v-layout v-widget spml-cell v-csslayout-spml-cell spml-property v-csslayout-spml-property span4 v-csslayout-span4">

This difference results in Watir navigating to a different parent and then jumping over to the adjacent field.

Knowing this, if you wanted to be as close to your XPath as possible, you could add the class locator to the initial div:

@browser.div(text: locator.strip, class: 'v-captiontext').parent.following_sibling(tag_name: 'div').input.following_sibling(tag_name: 'div').click

However, assuming there isn't any other text in these elements (ie exactly like the sample), Watir is already giving the top-level element. Therefore you could simply do:

@browser.div(text: locator.strip).div(class: 'v-filterselect-button').click

Upvotes: 2

Related Questions