2013-10-10 66 views
1

我是Watir世界的新人。我認爲我的問題很簡單,但我無法完成。如何動態創建元素定位器?

這裏是我的代碼:

names = Hash.new 
names[:text] = 'Image' 
puts names 
browser.a("#{names}").click 

我收到此錯誤:

'extract_selector': expected Hash or (:how, 'what'), got ["{:text=>\"Image\"}"] (ArgumentError) 

print正顯示出正確的值作爲"{:text=>"Image"}"

回答

2

如果你仔細閱讀錯誤消息 - * 'extract_selector':預期哈希或(:怎麼樣, '什麼')

*試試這個:

browser.a(:text => 'Image').click 
      :how  :what 

然後做(按OP的評論)

browser.a(names).click 
      Hash 

下面是完整的代碼:

require 'watir-webdriver' 

b = Watir::Browser.new 
b.goto "http://en.wikipedia.org/wiki/Ruby_(programming_language)" 
# <a href="/wiki/Just-in-time_compilation" title="Just-in-time compilation">just-in-time compilation</a> 
hsh = {:text => 'just-in-time compilation'} 
b.a(hsh).text # => "just-in-time compilation" 
+0

我的問題是我想創建:text =>'圖像'動態。我將在運行時獲取屬性名稱(文本)和值(圖像),然後我想使用它查找元素。 – Pratik

+0

@Pratik所以..鍵和值都是動態的? –

+0

是的。兩者都是動態的 – Pratik

0

它說它預計哈希,所以不要給它一個字符串:

browser.a(names).click