1616# under the License.
1717
1818"""The ActionChains implementation,"""
19+ from __future__ import annotations
20+
1921import warnings
22+ from typing import TYPE_CHECKING
23+ from typing import List
24+ from typing import Optional
25+ from typing import Union
2026
2127from selenium .webdriver .remote .webelement import WebElement
2228
2733from .actions .wheel_input import WheelInput
2834from .utils import keys_to_typing
2935
36+ if TYPE_CHECKING :
37+ from selenium .webdriver import Chrome
38+ from selenium .webdriver import Edge
39+ from selenium .webdriver import Firefox
40+ from selenium .webdriver import Ie
41+ from selenium .webdriver import Safari
42+
43+ AnyDriver = Union [Chrome , Firefox , Safari , Ie , Edge ]
44+
45+ AnyDevice = Union [PointerInput , KeyInput , WheelInput ]
46+
3047
3148class ActionChains :
3249 """ActionChains are a way to automate low level interactions such as mouse
@@ -61,7 +78,7 @@ class ActionChains:
6178 another.
6279 """
6380
64- def __init__ (self , driver , duration = 250 , devices = None ):
81+ def __init__ (self , driver : AnyDriver , duration : int = 250 , devices : Optional [ List [ AnyDevice ]] = None ) -> None :
6582 """Creates a new ActionChains.
6683
6784 :Args:
@@ -82,18 +99,18 @@ def __init__(self, driver, duration=250, devices=None):
8299 wheel = device
83100 self .w3c_actions = ActionBuilder (driver , mouse = mouse , keyboard = keyboard , wheel = wheel , duration = duration )
84101
85- def perform (self ):
102+ def perform (self ) -> None :
86103 """Performs all stored actions."""
87104 self .w3c_actions .perform ()
88105
89- def reset_actions (self ):
106+ def reset_actions (self ) -> None :
90107 """Clears actions that are already stored locally and on the remote
91108 end."""
92109 self .w3c_actions .clear_actions ()
93110 for device in self .w3c_actions .devices :
94111 device .clear_actions ()
95112
96- def click (self , on_element = None ) :
113+ def click (self , on_element = Optional [ WebElement ]) -> "ActionChains" :
97114 """Clicks an element.
98115
99116 :Args:
@@ -109,7 +126,7 @@ def click(self, on_element=None):
109126
110127 return self
111128
112- def click_and_hold (self , on_element = None ) :
129+ def click_and_hold (self , on_element = Optional [ WebElement ]) -> "ActionChains" :
113130 """Holds down the left mouse button on an element.
114131
115132 :Args:
@@ -124,7 +141,7 @@ def click_and_hold(self, on_element=None):
124141
125142 return self
126143
127- def context_click (self , on_element = None ) :
144+ def context_click (self , on_element = Optional [ WebElement ]) -> "ActionChains" :
128145 """Performs a context-click (right click) on an element.
129146
130147 :Args:
@@ -140,7 +157,7 @@ def context_click(self, on_element=None):
140157
141158 return self
142159
143- def double_click (self , on_element = None ) :
160+ def double_click (self , on_element = Optional [ WebElement ]) -> "ActionChains" :
144161 """Double-clicks an element.
145162
146163 :Args:
@@ -156,7 +173,7 @@ def double_click(self, on_element=None):
156173
157174 return self
158175
159- def drag_and_drop (self , source , target ) :
176+ def drag_and_drop (self , source : WebElement , target : WebElement ) -> "ActionChains" :
160177 """Holds down the left mouse button on the source element, then moves
161178 to the target element and releases the mouse button.
162179
@@ -168,7 +185,7 @@ def drag_and_drop(self, source, target):
168185 self .release (target )
169186 return self
170187
171- def drag_and_drop_by_offset (self , source , xoffset , yoffset ) :
188+ def drag_and_drop_by_offset (self , source : WebElement , xoffset : int , yoffset : int ) -> "ActionChains" :
172189 """Holds down the left mouse button on the source element, then moves
173190 to the target offset and releases the mouse button.
174191
@@ -182,7 +199,7 @@ def drag_and_drop_by_offset(self, source, xoffset, yoffset):
182199 self .release ()
183200 return self
184201
185- def key_down (self , value , element = None ) :
202+ def key_down (self , value : str , element = Optional [ WebElement ]) -> "ActionChains" :
186203 """Sends a key press only, without releasing it. Should only be used
187204 with modifier keys (Control, Alt and Shift).
188205
@@ -203,7 +220,7 @@ def key_down(self, value, element=None):
203220
204221 return self
205222
206- def key_up (self , value , element = None ) :
223+ def key_up (self , value : str , element = Optional [ WebElement ]) -> "ActionChains" :
207224 """Releases a modifier key.
208225
209226 :Args:
@@ -223,7 +240,7 @@ def key_up(self, value, element=None):
223240
224241 return self
225242
226- def move_by_offset (self , xoffset , yoffset ) :
243+ def move_by_offset (self , xoffset : int , yoffset : int ) -> "ActionChains" :
227244 """Moving the mouse to an offset from current mouse position.
228245
229246 :Args:
@@ -236,7 +253,7 @@ def move_by_offset(self, xoffset, yoffset):
236253
237254 return self
238255
239- def move_to_element (self , to_element ) :
256+ def move_to_element (self , to_element : WebElement ) -> "ActionChains" :
240257 """Moving the mouse to the middle of an element.
241258
242259 :Args:
@@ -248,7 +265,7 @@ def move_to_element(self, to_element):
248265
249266 return self
250267
251- def move_to_element_with_offset (self , to_element , xoffset , yoffset ) :
268+ def move_to_element_with_offset (self , to_element : WebElement , xoffset : int , yoffset : int ) -> "ActionChains" :
252269 """Move the mouse by an offset of the specified element. Offsets are
253270 relative to the in-view center point of the element.
254271
@@ -263,15 +280,15 @@ def move_to_element_with_offset(self, to_element, xoffset, yoffset):
263280
264281 return self
265282
266- def pause (self , seconds ) :
283+ def pause (self , seconds : Union [ float , int ]) -> "ActionChains" :
267284 """Pause all inputs for the specified duration in seconds."""
268285
269286 self .w3c_actions .pointer_action .pause (seconds )
270287 self .w3c_actions .key_action .pause (seconds )
271288
272289 return self
273290
274- def release (self , on_element = None ) :
291+ def release (self , on_element = Optional [ WebElement ]) -> "ActionChains" :
275292 """Releasing a held mouse button on an element.
276293
277294 :Args:
@@ -286,7 +303,7 @@ def release(self, on_element=None):
286303
287304 return self
288305
289- def send_keys (self , * keys_to_send ) :
306+ def send_keys (self , * keys_to_send : str ) -> "ActionChains" :
290307 """Sends keys to current focused element.
291308
292309 :Args:
@@ -301,7 +318,7 @@ def send_keys(self, *keys_to_send):
301318
302319 return self
303320
304- def send_keys_to_element (self , element , * keys_to_send ) :
321+ def send_keys_to_element (self , element : WebElement , * keys_to_send : str ) -> "ActionChains" :
305322 """Sends keys to an element.
306323
307324 :Args:
@@ -313,7 +330,7 @@ def send_keys_to_element(self, element, *keys_to_send):
313330 self .send_keys (* keys_to_send )
314331 return self
315332
316- def scroll_to_element (self , element : WebElement ):
333+ def scroll_to_element (self , element : WebElement ) -> "ActionChains" :
317334 """If the element is outside the viewport, scrolls the bottom of the
318335 element to the bottom of the viewport.
319336
@@ -324,7 +341,7 @@ def scroll_to_element(self, element: WebElement):
324341 self .w3c_actions .wheel_action .scroll (origin = element )
325342 return self
326343
327- def scroll_by_amount (self , delta_x : int , delta_y : int ):
344+ def scroll_by_amount (self , delta_x : int , delta_y : int ) -> "ActionChains" :
328345 """Scrolls by provided amounts with the origin in the top left corner
329346 of the viewport.
330347
@@ -336,7 +353,7 @@ def scroll_by_amount(self, delta_x: int, delta_y: int):
336353 self .w3c_actions .wheel_action .scroll (delta_x = delta_x , delta_y = delta_y )
337354 return self
338355
339- def scroll_from_origin (self , scroll_origin : ScrollOrigin , delta_x : int , delta_y : int ):
356+ def scroll_from_origin (self , scroll_origin : ScrollOrigin , delta_x : int , delta_y : int ) -> "ActionChains" :
340357 """Scrolls by provided amount based on a provided origin. The scroll
341358 origin is either the center of an element or the upper left of the
342359 viewport plus any offsets. If the origin is an element, and the element
@@ -364,7 +381,9 @@ def scroll_from_origin(self, scroll_origin: ScrollOrigin, delta_x: int, delta_y:
364381 )
365382 return self
366383
367- def scroll (self , x : int , y : int , delta_x : int , delta_y : int , duration : int = 0 , origin : str = "viewport" ):
384+ def scroll (
385+ self , x : int , y : int , delta_x : int , delta_y : int , duration : int = 0 , origin : str = "viewport"
386+ ) -> "ActionChains" :
368387 """Sends wheel scroll information to the browser to be processed.
369388
370389 :Args:
@@ -386,8 +405,8 @@ def scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int = 0,
386405
387406 # Context manager so ActionChains can be used in a 'with .. as' statements.
388407
389- def __enter__ (self ):
408+ def __enter__ (self ) -> "ActionChains" :
390409 return self # Return created instance of self.
391410
392- def __exit__ (self , _type , _value , _traceback ):
411+ def __exit__ (self , _type , _value , _traceback ) -> None :
393412 pass # Do nothing, does not require additional cleanup.
0 commit comments