您可以使用 selenium-webdriver 的操作构建器。但是,firefoxdriver 中似乎存在一个错误,导致目前无法正常工作(可能是issue 4863)。
这是使用 Chrome 的 JQuery 可选页面的工作示例:
require 'capybara'
require 'capybara/dsl'
include Capybara::DSL
#Use selenium-webdriver with chrome
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.current_driver = :selenium
#Go to the JQuery Selectable example page
Capybara.app_host = 'http://jqueryui.com/selectable/'
page.visit('')
#The controls are in a frame, so need to switch to it
within_frame 0 do
#Create a selenium-webdriver action builder
builder = page.driver.browser.action
#Hold control key down
builder.key_down(:control)
#Click all elements that you want, in this case we click all lis
#Note that you can retrieve the elements using capybara's
# standard methods. When passing them to the builder
# make sure to do .native
elements = page.all('ol#selectable li')
elements.each do |e|
builder.click(e.native)
end
#Release control key
builder.key_up(:control)
#Do the action setup
builder.perform
end