2015-06-15 49 views
-1

我想用Watir(Phantom JS)在文本框中輸入文本。但是,如果我不僅僅選擇課程,我怎麼能選擇文本框的ID?如何使用Watir(PhantomJS)選擇HTML ID?

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

回答

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