Sri
Sri

Reputation: 2273

how to find the text from the multiple same attribute in watir?

I want to find and click the Line Lo value from the following value. The following li had more than 100 records has same class name. How to find the Line Lo and click that value.

<div id="loc">
  <ul id="tab">
    <li class="criteria">
      <div class="bud">
        <div class="inner">
          <div class="attr">Code Lo</div>
        </div>
      </div>
    </li>
    <li class="criteria">
      <div class="bud">
        <div class="inner">
          <div class="attr">Line Lo</div>
        </div>
      </div>
    </li>
    <li class="criteria">
      <div class="bud">
        <div class="inner">
          <div class="attr">Add Lo</div>
        </div>
      </div>
    </li>
  </ul>
</div>

When I tried, browser.ul(id: "tab").li(class: "criteria").div(text: "Line Lo")

Got the failure error.

Unable to locate element, using(:class => "Criteria", :tag_name=>"li"

I tried different, different attributes to find the Text Line Lo and I failed.

Also, This element is like 45th, so is it possible to scroll down and flash this value before click?

Thanks in advance

Upvotes: 2

Views: 2625

Answers (2)

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

The original thing you tried

browser.ul(id: "tab").li(class: "criteria").div(text: "Line Lo")

failed because when there is more than one element that could match the criteria you specify, watir will use the first one it finds. So what that is asking watir to do would equate to the following in english

Find the first un-ordered list with the id 'tab', then inside that list find the first list item with the class 'criteria', then inside that list item, find a div with the text 'Line Lo'.

Since the first LI inside that list does not contain a div with that text, it fails.

To click the innermost div (based on comments, this is what you are trying to do)

browser.div(:class => "attr", :text => "Line Lo").click

aka Find me a div with the class "attr" AND the text "Line Lo", and then have the div click itself


these other options were presented when it was not clear exactly which div the OP was after, but I'm leaving them as they might be informative to other folks struggling with similar issues

To click the div that contains the one above (the parent).

browser.div(:class => "inner", :text => "Line Lo").click

if that parent div had no uniqueness (no class etc) you could still get to it like this

browser.div(:class => "attr", :text => "Line Lo").parent.click

To click the li tag that holds all that stuff

browser.li(:class => "criteria", :text => "Line Lo").click

For any of those, if you need to restrict to just looking inside that particular list, then specify like this

browser.ul(:id => "tab").li(:class => "criteria", :text => "Line Lo").click

Upvotes: 6

Justin Ko
Justin Ko

Reputation: 46836

You could try:

browser.ul(:id => 'tab').div(:class => 'attr', :text => 'Line Lo').click

When .click is used, it should get scrolled into view before being clicked.

Upvotes: 3

Related Questions