Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/actions/pointer_input.py
4036 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
from typing import Any
19
20
from selenium.common.exceptions import InvalidArgumentException
21
from selenium.webdriver.common.actions.input_device import InputDevice
22
from selenium.webdriver.common.actions.interaction import POINTER, POINTER_KINDS
23
from selenium.webdriver.remote.webelement import WebElement
24
25
26
class 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
80
81