Path: blob/trunk/py/selenium/webdriver/common/actions/wheel_input.py
1864 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.16from typing import Union1718from selenium.webdriver.remote.webelement import WebElement1920from . import interaction21from .input_device import InputDevice222324class ScrollOrigin:25def __init__(self, origin: Union[str, WebElement], x_offset: int, y_offset: int) -> None:26self._origin = origin27self._x_offset = x_offset28self._y_offset = y_offset2930@classmethod31def from_element(cls, element: WebElement, x_offset: int = 0, y_offset: int = 0):32return cls(element, x_offset, y_offset)3334@classmethod35def from_viewport(cls, x_offset: int = 0, y_offset: int = 0):36return cls("viewport", x_offset, y_offset)3738@property39def origin(self) -> Union[str, WebElement]:40return self._origin4142@property43def x_offset(self) -> int:44return self._x_offset4546@property47def y_offset(self) -> int:48return self._y_offset495051class WheelInput(InputDevice):52def __init__(self, name) -> None:53super().__init__(name=name)54self.name = name55self.type = interaction.WHEEL5657def encode(self) -> dict:58return {"type": self.type, "id": self.name, "actions": self.actions}5960def create_scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int, origin) -> None:61if isinstance(origin, WebElement):62origin = {"element-6066-11e4-a52e-4f735466cecf": origin.id}63self.add_action(64{65"type": "scroll",66"x": x,67"y": y,68"deltaX": delta_x,69"deltaY": delta_y,70"duration": duration,71"origin": origin,72}73)7475def create_pause(self, pause_duration: Union[int, float] = 0) -> None:76self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})777879