Sig
Sig

Reputation: 5928

WATIR - Setting a checkbox without accessing it directly

I'm using Watir to automate some web chore and I need to set a checkbox which is located as the previous tag of the parent of a div with some known text.

HTML looks like

<div>
 ...
 <input type='checkbox'>
 <div>
  <div class='text'>text I know</div>
  ...
  </div>
</div>

I'm able to locate the checkbox with

browser.div(class: 'text', text: /text I know/).parent.preceding_sibling

As evidence of that, if I run puts browser.div(class: 'text', text: /text I know/).parent.preceding_sibling/.html I get "<input type=\"checkbox\">".

However, if I try

browser.div(class: 'text', text: /text I know/).parent.preceding_sibling.set

Which, as far as I know, is the way to set checkboxes with Watir, I get

method_missing': undefined methodset' for # (NoMethodError)

Why do I get a Watir::HTMLElement and not a Watir::CheckBox instance? Is there a way to get a checkbox instead?

Upvotes: 0

Views: 67

Answers (1)

Rajagopalan
Rajagopalan

Reputation: 6064

That's because, the following code returns element object

browser.div(class: 'text', text: /text I know/).parent.preceding_sibling

So set method is not defined for that object. set method is defined for CheckBox class. So you try the following code, it will work for you.

browser.div(class: 'text', text: /text I know/).parent.preceding_sibling.to_subtype.set

to_subtype returns the corresponding object for that tag. So your set method would work.

Upvotes: 1

Related Questions