如何使用Watir(PhantomJS)来选择HTML ID?

时间:2015-06-15 23:55:14

标签: ruby-on-rails ruby phantomjs nokogiri watir-webdriver

我正在尝试使用Watir(Phantom JS)在文本字段中输入文本。但是,如果我不只是在课堂上选择,我该如何选择文本字段的ID呢?

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

1 个答案:

答案 0 :(得分:0)

假设您有以下HTML:

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

通过使用<input>标记的属性作为定位符,您可以在text_field中输入字符串。例如,您可以使用以下之一:

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

据我所知,set?方法仅适用于复选框和单选按钮。如果要在text_field中检索字符串,可以使用value方法:

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