Path: blob/trunk/py/selenium/webdriver/common/actions/wheel_input.py
4076 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617from selenium.webdriver.common.actions import interaction18from selenium.webdriver.common.actions.input_device import InputDevice19from selenium.webdriver.remote.webelement import WebElement202122class ScrollOrigin:23def __init__(self, origin: str | WebElement, x_offset: int, y_offset: int) -> None:24self._origin = origin25self._x_offset = x_offset26self._y_offset = y_offset2728@classmethod29def from_element(cls, element: WebElement, x_offset: int = 0, y_offset: int = 0):30return cls(element, x_offset, y_offset)3132@classmethod33def from_viewport(cls, x_offset: int = 0, y_offset: int = 0):34return cls("viewport", x_offset, y_offset)3536@property37def origin(self) -> str | WebElement:38return self._origin3940@property41def x_offset(self) -> int:42return self._x_offset4344@property45def y_offset(self) -> int:46return self._y_offset474849class WheelInput(InputDevice):50def __init__(self, name) -> None:51super().__init__(name=name)52self.name = name53self.type = interaction.WHEEL5455def encode(self) -> dict:56return {"type": self.type, "id": self.name, "actions": self.actions}5758def create_scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int, origin) -> None:59if isinstance(origin, WebElement):60origin = {"element-6066-11e4-a52e-4f735466cecf": origin.id}61self.add_action(62{63"type": "scroll",64"x": x,65"y": y,66"deltaX": delta_x,67"deltaY": delta_y,68"duration": duration,69"origin": origin,70}71)7273def create_pause(self, pause_duration: int | float = 0) -> None:74self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})757677