Path: blob/trunk/py/selenium/webdriver/common/actions/pointer_actions.py
4012 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.interaction import Interaction19from selenium.webdriver.common.actions.mouse_button import MouseButton20from selenium.webdriver.common.actions.pointer_input import PointerInput21from selenium.webdriver.remote.webelement import WebElement222324class PointerActions(Interaction):25def __init__(self, source: PointerInput | None = None, duration: int = 250):26"""Initialize a new PointerActions instance.2728Args:29source: Optional PointerInput instance. If not provided, a default30mouse PointerInput will be created.31duration: Override the default 250 msecs of DEFAULT_MOVE_DURATION32in the source.33"""34if source is None:35source = PointerInput(interaction.POINTER_MOUSE, "mouse")36self.source = source37self._duration = duration38super().__init__(source)3940def pointer_down(41self,42button=MouseButton.LEFT,43width=None,44height=None,45pressure=None,46tangential_pressure=None,47tilt_x=None,48tilt_y=None,49twist=None,50altitude_angle=None,51azimuth_angle=None,52):53self._button_action(54"create_pointer_down",55button=button,56width=width,57height=height,58pressure=pressure,59tangential_pressure=tangential_pressure,60tilt_x=tilt_x,61tilt_y=tilt_y,62twist=twist,63altitude_angle=altitude_angle,64azimuth_angle=azimuth_angle,65)66return self6768def pointer_up(self, button=MouseButton.LEFT):69self._button_action("create_pointer_up", button=button)70return self7172def move_to(73self,74element,75x=0,76y=0,77width=None,78height=None,79pressure=None,80tangential_pressure=None,81tilt_x=None,82tilt_y=None,83twist=None,84altitude_angle=None,85azimuth_angle=None,86):87if not isinstance(element, WebElement):88raise AttributeError("move_to requires a WebElement")8990self.source.create_pointer_move(91origin=element,92duration=self._duration,93x=int(x),94y=int(y),95width=width,96height=height,97pressure=pressure,98tangential_pressure=tangential_pressure,99tilt_x=tilt_x,100tilt_y=tilt_y,101twist=twist,102altitude_angle=altitude_angle,103azimuth_angle=azimuth_angle,104)105return self106107def move_by(108self,109x,110y,111width=None,112height=None,113pressure=None,114tangential_pressure=None,115tilt_x=None,116tilt_y=None,117twist=None,118altitude_angle=None,119azimuth_angle=None,120):121self.source.create_pointer_move(122origin=interaction.POINTER,123duration=self._duration,124x=int(x),125y=int(y),126width=width,127height=height,128pressure=pressure,129tangential_pressure=tangential_pressure,130tilt_x=tilt_x,131tilt_y=tilt_y,132twist=twist,133altitude_angle=altitude_angle,134azimuth_angle=azimuth_angle,135)136return self137138def move_to_location(139self,140x,141y,142width=None,143height=None,144pressure=None,145tangential_pressure=None,146tilt_x=None,147tilt_y=None,148twist=None,149altitude_angle=None,150azimuth_angle=None,151):152self.source.create_pointer_move(153origin="viewport",154duration=self._duration,155x=int(x),156y=int(y),157width=width,158height=height,159pressure=pressure,160tangential_pressure=tangential_pressure,161tilt_x=tilt_x,162tilt_y=tilt_y,163twist=twist,164altitude_angle=altitude_angle,165azimuth_angle=azimuth_angle,166)167return self168169def click(self, element: WebElement | None = None, button=MouseButton.LEFT):170if element:171self.move_to(element)172self.pointer_down(button)173self.pointer_up(button)174return self175176def context_click(self, element: WebElement | None = None):177return self.click(element=element, button=MouseButton.RIGHT)178179def click_and_hold(self, element: WebElement | None = None, button=MouseButton.LEFT):180if element:181self.move_to(element)182self.pointer_down(button=button)183return self184185def release(self, button=MouseButton.LEFT):186self.pointer_up(button=button)187return self188189def double_click(self, element: WebElement | None = None):190if element:191self.move_to(element)192self.pointer_down(MouseButton.LEFT)193self.pointer_up(MouseButton.LEFT)194self.pointer_down(MouseButton.LEFT)195self.pointer_up(MouseButton.LEFT)196return self197198def pause(self, duration: float = 0):199self.source.create_pause(duration)200return self201202def _button_action(self, action, **kwargs):203meth = getattr(self.source, action)204meth(**kwargs)205return self206207208