How to use Watir (PhantomJS) to select on HTML ID?

152 views Asked by At

I am trying to use Watir (Phantom JS) to input a text in a textfield. But how can I select the textfield's ID if I am not just selecting on the class?

browser.text_field(id: "message").set     # would this work? or do I need
browser.text_field(name: "#message").set? # Can I drop the "#" ? 
1

There are 1 answers

0
orde On

Assume you have the following HTML:

<input type="text" id="message" name="name">

By using an attribute of the <input> tag as a locator, you can input a string into the text_field. For example, you could use one of the following:

b.text_field(id: "message").set "this is the message"
b.text_field(name: "name").set  "this is the name"

As far as I know, the set? method only works for checkboxes and radio buttons. If you want to retrieve the string in the text_field, you could use the valuemethod:

b.text_field(id: "message").set "this is the message"
puts b.text_field(id: "message").value
#=> this is the message