public
Last active

Like ActionChains.py but it just does what you want. #selenium2 #webdriver #python

  • Download Gist
actor.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
# Copyright 2012 Randall Bohn
# based on ActionChains.py:
# Copyright 2011 WebDriver committers
# Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
"""
The Actor implementation
"""
from selenium.webdriver.remote.command import Command
from selenium.webdriver.common.keys import Keys
 
class Actor(object):
"""
Generate user actions.
Build your own functions that call the Actor to do stuff.
"""
 
def __init__(self, driver):
"""
Creates a new Actor.
:Args:
- driver: The WebDriver instance which performs user actions.
"""
self._driver = driver
def click(self, on_element=None):
"""
Clicks an element.
:Args:
- on_element: The element to click.
If None, clicks on current mouse position.
"""
if on_element: self.move_to_element(on_element)
self._driver.execute(Command.CLICK, {'button': 0})
return self
 
def click_and_hold(self, on_element):
"""
Holds down the left mouse button on an element.
:Args:
- on_element: The element to mouse down.
If None, clicks on current mouse position.
"""
if on_element: self.move_to_element(on_element)
self._driver.execute(Command.MOUSE_DOWN, {})
return self
 
def context_click(self, on_element):
"""
Performs a context-click (right click) on an element.
:Args:
- on_element: The element to context-click.
If None, clicks on current mouse position.
"""
if on_element: self.move_to_element(on_element)
self._driver.execute(Command.CLICK, {'button': 2})
return self
 
def double_click(self, on_element):
"""
Double-clicks an element.
:Args:
- on_element: The element to double-click.
If None, clicks on current mouse position.
"""
if on_element: self.move_to_element(on_element)
self._driver.execute(Command.DOUBLE_CLICK, {})
return self
 
def drag_and_drop(self, source, target):
"""Holds down the left mouse button on the source element,
then moves to the target element and releases the mouse button.
:Args:
- source: The element to mouse down.
- target: The element to mouse up.
"""
self.click_and_hold(source)
self.release(target)
return self
 
def drag_and_drop_by_offset(self, source, xoffset, yoffset):
"""
Holds down the left mouse button on the source element,
then moves to the target element and releases the mouse button.
:Args:
- source: The element to mouse down.
- xoffset: X offset to move to.
- yoffset: Y offset to move to.
"""
self.click_and_hold(source)
self.move_by_offset(xoffset, yoffset)
self.release(source)
return self
 
def key_down(self, value, element=None):
"""Sends a key press only, without releasing it.
Should only be used with modifier keys (Control, Alt and Shift).
 
:Args:
- key: The modifier key to send. Values are defined in Keys class.
- target: The element to send keys.
If None, sends a key to current focused element.
"""
typing = []
for val in value:
if isinstance(val, Keys):
typing.append(val)
elif isinstance(val, int):
val = str(val)
for i in range(len(val)):
typing.append(val[i])
else:
for i in range(len(val)):
typing.append(val[i])
 
if element: self.click(element)
self._driver.execute(Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {
"value": typing })
return self
 
def key_up(self, value, element=None):
"""
Releases a modifier key.
 
:Args:
- key: The modifier key to send. Values are defined in Keys class.
- target: The element to send keys.
If None, sends a key to current focused element.
"""
typing = []
for val in value:
if isinstance(val, Keys):
typing.append(val)
elif isinstance(val, int):
val = str(val)
for i in range(len(val)):
typing.append(val[i])
else:
for i in range(len(val)):
typing.append(val[i])
 
if element: self.click(element)
self._driver.execute(Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {
"value": typing })
return self
 
def move_by_offset(self, xoffset, yoffset):
"""
Moving the mouse to an offset from current mouse position.
 
:Args:
- xoffset: X offset to move to.
- yoffset: Y offset to move to.
"""
self._driver.execute(Command.MOVE_TO, {
'xoffset': xoffset,
'yoffset': yoffset})
return self
 
def move_to_element(self, to_element):
"""
Moving the mouse to the middle of an element.
 
:Args:
- to_element: The element to move to.
"""
self._driver.execute(Command.MOVE_TO, {'element': to_element.id})
return self
 
def move_to_element_with_offset(self, to_element, xoffset, yoffset):
"""
Move the mouse by an offset of the specificed element.
Offsets are relative to the top-left corner of the element.
 
:Args:
- to_element: The element to move to.
- xoffset: X offset to move to.
- yoffset: Y offset to move to.
"""
self._driver.execute(Command.MOVE_TO, {
'element': to_element.id,
'xoffset': xoffset,
'yoffset': yoffset})
return self
 
def release(self, on_element):
"""
Releasing a held mouse button.
 
:Args:
- on_element: The element to mouse up.
"""
if on_element: self.move_to_element(on_element)
self._driver.execute(Command.MOUSE_UP, {})
return self
 
def send_keys(self, *keys_to_send):
"""Sends keys to current focused element.
 
:Args:
- keys_to_send: The keys to send.
"""
self._driver.switch_to_active_element().send_keys(*keys_to_send)
return self
 
def send_keys_to_element(self, element, *keys_to_send):
"""
Sends keys to an element.
 
:Args:
- element: The element to send keys.
- keys_to_send: The keys to send.
"""
element.send_keys(*keys_to_send)
return self

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.