Mouse & Keyboard
Mouse
page.mouse
scroll_to(x, y)
Scroll page to a given x, y
- x
Integerthe pixel along the horizontal axis of the document that you want displayed in the upper left - y
Integerthe pixel along the vertical axis of the document that you want displayed in the upper left
page.go_to("https://www.google.com/search?q=Ruby+headless+driver+for+Capybara")
page.mouse.scroll_to(0, 400)
click(**options) : Mouse
Click given coordinates, fires mouse move, down and up events.
- options
Hash- :x
Integer - :y
Integer - :delay
Floatdefaults to 0. Delay between mouse down and mouse up events - :button
Symbol:left | :right, defaults to :left - :count
Integerdefaults to 1 - :modifiers
Integerbitfield for key modifiers. Seekeyboard.modifiers. If omitted, uses the keys held bykeyboard.hold, or none outside of it
- :x
down(**options) : Mouse
Mouse down for given coordinates.
- options
Hash- :button
Symbol:left | :right, defaults to :left - :count
Integerdefaults to 1 - :modifiers
Integerbitfield for key modifiers. Seekeyboard.modifiers. If omitted, uses the keys held bykeyboard.hold, or none outside of it
- :button
up(**options) : Mouse
Mouse up for given coordinates.
- options
Hash- :button
Symbol:left | :right, defaults to :left - :count
Integerdefaults to 1 - :modifiers
Integerbitfield for key modifiers. Seekeyboard.modifiers. If omitted, uses the keys held bykeyboard.hold, or none outside of it
- :button
move(x:, y:, steps: 1, modifiers: nil) : Mouse
Mouse move to given x and y.
- options
Hash- :x
Integer - :y
Integer - :steps
Integerdefaults to 1. Sends intermediate mousemove events. - :modifiers
Integerbitfield for key modifiers. Seekeyboard.modifiers. If omitted, uses the keys held bykeyboard.hold, or none outside of it
- :x
Keyboard
page.keyboard
down(key) : Keyboard
Dispatches a keydown event.
- key
String|SymbolName of key such as "a", :enter, :backspace. RaisesArgumentErrorfor a modifier, useholdinstead
up(key) : Keyboard
Dispatches a keyup event.
- key
String|SymbolName of key such as "b", :enter, :backspace. RaisesArgumentErrorfor a modifier, useholdinstead
type(*keys) : Keyboard
Sends a keydown, keypress/input, and keyup event for each character in the text.
- text
String|Array<String> | Array<Symbol>A text to type into a focused element,[:Shift, "s"], "tring"
hold(*keys) : Object
Holds modifier keys down while the block runs. Every keyboard and mouse event inside the block carries them, and they are released when the block returns or raises. Returns the block's value.
- keys
Symbol:alt | :ctrl | :control | :meta | :command | :shift
page.keyboard.hold(:shift) do
page.mouse.move(x: 10, y: 10).down.move(x: 200, y: 200, steps: 10).up
end
held_modifiers : Integer
Modifier keys currently pressed by hold, in the same format as modifiers returns. It's 0 outside a hold block.
page.keyboard.hold(:shift) { page.keyboard.held_modifiers } # => 8
modifiers(keys) : Integer
Returns bitfield for a given keys
- keys
Array<Symbol>:alt | :ctrl | :command | :shift
Examples
# Type text into an input
page.go_to("https://google.com")
input = page.at_css("input[name='q']")
input.focus
page.keyboard.type("Hello World")
# Press Enter
page.keyboard.type(:Enter)
# Use keyboard shortcuts
page.keyboard.hold(:shift) { page.keyboard.type("hello") }