1

我有一个包含位置列表项的页面对象。

select_list(:locations, :id => 'locations)

我想要一个位置列表,然后选择其中一个。就像是:

def select_item_different_than d_item    
 list_items = :locations.items #This is wrong, but you get the point
 list_items.each do |item|
   if item != d_item
     item.select
     return
   end
 end
end

非常感谢 :)

4

1 回答 1

1

选择列表元素具有options返回其选项元素数组的方法。您可以遍历数组并将它们与d_item.

该方法可以是:

def select_item_different_than d_item    
  list_items = locations_element.options
  list_items.each do |item|
    if item.text != d_item
      item.click
      return
    end
  end
end 

请注意,需要进行以下更改:

  1. 选择列表元素由locations_element而不是检索:locations
  2. 选项列表由options而不是检索items
  3. 在 if 语句item != d_item中更改为item.text != d_item. 我的假设是您想要比较选项的文本并且d_item是一个字符串。
  4. 选项元素没有select方法。相反,请使用该click方法。

就个人而言,我认为该方法可能更清楚:

def select_item_different_than d_item    
  locations_element
    .options
    .find{ |option| option.text != d_item }
    .click
end
于 2014-07-21T02:31:25.877 回答