MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / PointerInput

Class PointerInput

py/selenium/webdriver/common/actions/pointer_input.py:26–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24
25
26class PointerInput(InputDevice):
27 DEFAULT_MOVE_DURATION = 250
28
29 def __init__(self, kind, name):
30 super().__init__()
31 if kind not in POINTER_KINDS:
32 raise InvalidArgumentException(f"Invalid PointerInput kind '{kind}'")
33 self.type = POINTER
34 self.kind = kind
35 self.name = name
36
37 def create_pointer_move(
38 self,
39 duration=DEFAULT_MOVE_DURATION,
40 x: float = 0,
41 y: float = 0,
42 origin: WebElement | None = None,
43 **kwargs,
44 ):
45 action = {"type": "pointerMove", "duration": duration, "x": x, "y": y, **kwargs}
46 if isinstance(origin, WebElement):
47 action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id}
48 elif origin is not None:
49 action["origin"] = origin
50 self.add_action(self._convert_keys(action))
51
52 def create_pointer_down(self, **kwargs):
53 data = {"type": "pointerDown", "duration": 0, **kwargs}
54 self.add_action(self._convert_keys(data))
55
56 def create_pointer_up(self, button):
57 self.add_action({"type": "pointerUp", "duration": 0, "button": button})
58
59 def create_pointer_cancel(self):
60 self.add_action({"type": "pointerCancel"})
61
62 def create_pause(self, pause_duration: int | float = 0) -> None:
63 self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})
64
65 def encode(self):
66 return {"type": self.type, "parameters": {"pointerType": self.kind}, "id": self.name, "actions": self.actions}
67
68 def _convert_keys(self, actions: dict[str, Any]):
69 out = {}
70 for k, v in actions.items():
71 if v is None:
72 continue
73 if k in ("x", "y"):
74 out[k] = int(v)
75 continue
76 splits = k.split("_")
77 new_key = splits[0] + "".join(v.title() for v in splits[1:])
78 out[new_key] = v
79 return out

Calls

no outgoing calls