Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/actions/wheel_input.py
4076 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 selenium.webdriver.common.actions import interaction
19
from selenium.webdriver.common.actions.input_device import InputDevice
20
from selenium.webdriver.remote.webelement import WebElement
21
22
23
class ScrollOrigin:
24
def __init__(self, origin: str | WebElement, x_offset: int, y_offset: int) -> None:
25
self._origin = origin
26
self._x_offset = x_offset
27
self._y_offset = y_offset
28
29
@classmethod
30
def from_element(cls, element: WebElement, x_offset: int = 0, y_offset: int = 0):
31
return cls(element, x_offset, y_offset)
32
33
@classmethod
34
def from_viewport(cls, x_offset: int = 0, y_offset: int = 0):
35
return cls("viewport", x_offset, y_offset)
36
37
@property
38
def origin(self) -> str | WebElement:
39
return self._origin
40
41
@property
42
def x_offset(self) -> int:
43
return self._x_offset
44
45
@property
46
def y_offset(self) -> int:
47
return self._y_offset
48
49
50
class WheelInput(InputDevice):
51
def __init__(self, name) -> None:
52
super().__init__(name=name)
53
self.name = name
54
self.type = interaction.WHEEL
55
56
def encode(self) -> dict:
57
return {"type": self.type, "id": self.name, "actions": self.actions}
58
59
def create_scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int, origin) -> None:
60
if isinstance(origin, WebElement):
61
origin = {"element-6066-11e4-a52e-4f735466cecf": origin.id}
62
self.add_action(
63
{
64
"type": "scroll",
65
"x": x,
66
"y": y,
67
"deltaX": delta_x,
68
"deltaY": delta_y,
69
"duration": duration,
70
"origin": origin,
71
}
72
)
73
74
def create_pause(self, pause_duration: int | float = 0) -> None:
75
self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})
76
77