21
21
from selenium .webdriver .remote .command import Command
22
22
23
23
from .utils import keys_to_typing
24
+ from .actions .action_builder import ActionBuilder
24
25
25
26
26
27
class ActionChains (object ):
@@ -65,13 +66,25 @@ def __init__(self, driver):
65
66
"""
66
67
self ._driver = driver
67
68
self ._actions = []
69
+ if self ._driver .w3c :
70
+ self .w3c_actions = ActionBuilder (driver )
71
+
68
72
69
73
def perform (self ):
70
74
"""
71
75
Performs all stored actions.
72
76
"""
73
- for action in self ._actions :
74
- action ()
77
+ if self ._driver .w3c :
78
+ self .w3c_actions .perform ()
79
+ else :
80
+ for action in self ._actions :
81
+ action ()
82
+
83
+ def reset_actions (self ):
84
+ """
85
+ Clears actions that are already stored on the remote end.
86
+ """
87
+ self ._driver .execute (Command .W3C_CLEAR_ACTIONS )
75
88
76
89
def click (self , on_element = None ):
77
90
"""
@@ -174,9 +187,12 @@ def key_down(self, value, element=None):
174
187
"""
175
188
if element :
176
189
self .click (element )
177
- self ._actions .append (lambda : self ._driver .execute (
178
- Command .SEND_KEYS_TO_ACTIVE_ELEMENT ,
179
- {"value" : keys_to_typing (value )}))
190
+ if self ._driver .w3c :
191
+ self .w3c_actions .key_action .key_down (value )
192
+ else :
193
+ self ._actions .append (lambda : self ._driver .execute (
194
+ Command .SEND_KEYS_TO_ACTIVE_ELEMENT ,
195
+ {"value" : keys_to_typing (value )}))
180
196
return self
181
197
182
198
def key_up (self , value , element = None ):
@@ -195,9 +211,12 @@ def key_up(self, value, element=None):
195
211
"""
196
212
if element :
197
213
self .click (element )
198
- self ._actions .append (lambda : self ._driver .execute (
199
- Command .SEND_KEYS_TO_ACTIVE_ELEMENT ,
200
- {"value" : keys_to_typing (value )}))
214
+ if self ._driver .w3c :
215
+ self .w3c_actions .key_action .key_up (value )
216
+ else :
217
+ self ._actions .append (lambda : self ._driver .execute (
218
+ Command .SEND_KEYS_TO_ACTIVE_ELEMENT ,
219
+ {"value" : keys_to_typing (value )}))
201
220
return self
202
221
203
222
def move_by_offset (self , xoffset , yoffset ):
@@ -263,8 +282,11 @@ def send_keys(self, *keys_to_send):
263
282
- keys_to_send: The keys to send. Modifier keys constants can be found in the
264
283
'Keys' class.
265
284
"""
266
- self ._actions .append (lambda : self ._driver .execute (
267
- Command .SEND_KEYS_TO_ACTIVE_ELEMENT , {'value' : keys_to_typing (keys_to_send )}))
285
+ if self ._driver .w3c :
286
+ self .w3c_actions .key_action .send_keys (keys_to_send )
287
+ else :
288
+ self ._actions .append (lambda : self ._driver .execute (
289
+ Command .SEND_KEYS_TO_ACTIVE_ELEMENT , {'value' : keys_to_typing (keys_to_send )}))
268
290
return self
269
291
270
292
def send_keys_to_element (self , element , * keys_to_send ):
@@ -276,7 +298,10 @@ def send_keys_to_element(self, element, *keys_to_send):
276
298
- keys_to_send: The keys to send. Modifier keys constants can be found in the
277
299
'Keys' class.
278
300
"""
279
- self ._actions .append (lambda : element .send_keys (* keys_to_send ))
301
+ if self ._driver .w3c :
302
+ self .w3c_actions .key_action .send_keys (keys_to_send , element = element )
303
+ else :
304
+ self ._actions .append (lambda : element .send_keys (* keys_to_send ))
280
305
return self
281
306
282
307
# Context manager so ActionChains can be used in a 'with .. as' statements.
0 commit comments