您可以使用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
你確定你需要從字面上按下按鍵?硒在隱藏/只讀/禁用的元素上工作,因此您可以在日曆後面輸入數據 – 2013-03-12 15:30:58
e.metaKey for mac支持 – 2013-03-12 15:31:26
使用jQuery可選,用於選擇需要使用Ctrl修飾符的多個項目。我認爲硒需要按字面上按Ctrl .... – 2013-03-12 15:53:02