mokr.input

Submodules

Package Contents

Classes

Dialog

Dialog objects are initialised by the parent mokr.browser.Page

Keyboard

Class to allow sending key events to emulate a keyboard.

Mouse

Class to emulate mouse movement.

Touchscreen

Class to emulate touchscreen.

class mokr.input.Dialog(client: mokr.connection.DevtoolsConnection, dialog_type: str, message: str, default_value: str | None = None)

Dialog objects are initialised by the parent mokr.browser.Page when a dialog event is triggered.

Parameters:
  • client (DevtoolsConnection) – A mokr.connection.DevtoolsConnection spawned by the parent mokr.browser.Page.

  • dialog_type (str) – Dialog type from the triggering event’s type.

  • message (str) – Message in the dialog.

  • default_value (str | None, optional) – Default prompt that the dialog spawned with. Defaults to None.

Example:

async def close_dialog(dialog):
    print(dialog.message)
    await dialog.dismiss()
page.on("dialog", close_dialog)
property kind: str

Get the remote Dialog event type. One of “alert”, “beforeunload, “confirm”, “prompt, or “”.

property message: str

Get the message the dialog spawned with.

property default_value: str

Get default prompt value, if remote dialog type is “prompt”. Otherwise, get “”.

async accept(prompt_text: str = '') None

Accept the remote dialog. Can optionally accept with the given prompt_text, if Dialog.type is “prompt”.

Parameters:

prompt_text (str, optional) – _description_. Defaults to ‘’.

async dismiss() None

Dismiss the remote dialog.

class mokr.input.Keyboard(client: mokr.connection.DevtoolsConnection)

Class to allow sending key events to emulate a keyboard.

Use Keyboard.type("text") to type text normally; this will dispacth multiple key events for each character in the given text.

For finer control, use Keyboard.down, Keyboard.up and Keyboard.send_character.

Special keys and modifier key can be sent. To view all key names and their associated event payload, use Keyboard.key_definitions.

Parameters:

client (DevtoolsConnection) – Remote DevtoolsConnection instance.

property key_definitions: dict[str, dict[str, str | int]]

Dictionary of key names and their corresponding event codes.

async down(key: str, text: str | None = None) None

Send a “keyDown” event with the given key. Does not automatically send a “keyUp” event, use Keyboard.up for that.

Modifier keys effect this method, meaning sending Keyboard.down("shift") and then Keyboard.down("m") will type an uppercase “M”.

Parameters:
  • key (str) – Name of key. See Keyboard.key_definitions.

  • text (str | None, optional) – Force an “input” event to be sent, as well. Defaults to None.

async up(key: str) None

Send a “KeyUp” event with the given key.

Parameters:
  • key (str) – Name of key. See Keyboard.key_definitions.

  • text (str | None, optional) – Force an “input” event to be sent, as well. Defaults to None.

async send_character(char: str) None

Send a character char into the parent mokr.frame.Frame. Does not send “keyDown” or “keyUp” event, only sends “keyPress” and “input”.

Modifier keys do not effect this method, meaning sending Keyboard.down("shift") and then Keyboard.send_character("m") will type a lowercase “m”.

Parameters:

char (str) – Character to send, does not accept special characters.

async type_text(text: str, delay: int | float = 0) None

Type characters into the page; whatever element is focused will receive the sent input.

Note that modifier keys do not alter text case, meaning sending Keyboard.down("shift") and typing Keyboard.type_text("mokr") will not type “MOKR”.

Parameters:
  • text (str) – Text to type.

  • delay (int | float, optional) – Time in milliseconds to wait between each character typed. Defaults to 0.

async press(key: str, delay: int | float | None = None) None

Press the given key by sending Keyboard.down and Keyboard.up events, with the given delay in between.

Modifier keys effect this method, meaning sending Keyboard.down("shift") and then Keyboard.press("m") will type an uppercase “M”.

Parameters:
  • key (str) – Name of key. See Keyboard.key_definitions.

  • delay (int | float | None, optional) – Time in milliseconds to “hold” the key down. Defaults to None.

class mokr.input.Mouse(client: mokr.connection.DevtoolsConnection, keyboard: mokr.input.keyboard.Keyboard)

Class to emulate mouse movement.

The Mouse’s remote position is measured in pixels, with origin at the top-left corner of the viewport.

Parameters:
  • client (DevtoolsConnection) – Remote DevtoolsConnection instance.

  • keyboard (Keyboard) – Active mokr.input.Keyboard from the parent mokr.input.Keyboard. Passes active modifiers such as CTRL.

async move(x: float, y: float, steps: int = 1) None

Move the mouse to target coordinates (x, y), sending intermittent events along the trail for each step in steps.

Parameters:
  • x (float) – Target X coordinate.

  • y (float) – Target Y coordinate.

  • steps (int, optional) – Number of times to stop along the path. Defaults to 1 (final destination only).

async down(button: Literal[left, right, middle] = 'left', click_count: int = 1) None

Send a “mousePressed” event with given mouse button, repeat for click_count number of times.

Parameters:
  • button (Literal["left", "right", "middle"], optional) – Mouse button to send with. Defaults to “left”.

  • click_count (int, optional) – Number of click events to send. Defaults to 1.

async up(button: Literal[left, right, middle] = 'left', click_count: int = 1) None

Send a “mouseReleased” event with given mouse button, repeat for click_count number of times.

Parameters:
  • button (Literal["left", "right", "middle"], optional) – Mouse button to send with. Defaults to “left”.

  • click_count (int, optional) – Number of click events to send. Defaults to 1.

async click(x: float, y: float, button: Literal[left, right, middle] = 'left', click_count: int = 1, delay: int | float | None = 1000) None

Move the pointer to the target coordinates and click.

Shortcut to running Mouse.move then Mouse.down then Mouse.up.

Parameters:
  • x (float) – X coordinate to move to.

  • y (float) – Y coordinate to move to.

  • button (Literal["left", "right", "middle"], optional) – Mouse button to click with. Defaults to “left”.

  • click_count (int, optional) – Number of clicks to run. Defaults to 1.

  • delay (int | float | None, optional) – Time in milliseconds to wait before each click. Defaults to 1000.

class mokr.input.Touchscreen(client: mokr.connection.DevtoolsConnection, keyboard: mokr.input.keyboard.Keyboard)

Class to emulate touchscreen.

Parameters:
  • client (DevtoolsConnection) – Remote DevtoolsConnection instance.

  • keyboard (Keyboard) – Active mokr.input.Keyboard from the parent mokr.input.Keyboard. Passes active modifiers such as CTRL.

async tap(x: float, y: float) None

Send touch down and up events to the center of the target coordinates.

Parameters:
  • x (float) – X coordinate to move to.

  • y (float) – Y coordinate to move to.