selenium.webdriver.common.action_chains#
The ActionChains implementation.
Classes
|
Automate low-level interactions like mouse movements, button actions, key presses, and context menus. |
- class ActionChains(driver, duration=250, devices=None)[source]#
Bases:
objectAutomate low-level interactions like mouse movements, button actions, key presses, and context menus.
ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop.
- Generate user actions.
When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up.
ActionChains can be used in a chain pattern:
menu = driver.find_element(By.CSS_SELECTOR, ".nav") hidden_submenu = driver.find_element(By.CSS_SELECTOR, ".nav #submenu1") ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
Or actions can be queued up one by one, then performed.:
menu = driver.find_element(By.CSS_SELECTOR, ".nav") hidden_submenu = driver.find_element(By.CSS_SELECTOR, ".nav #submenu1") actions = ActionChains(driver) actions.move_to_element(menu) actions.click(hidden_submenu) actions.perform()
Either way, the actions are performed in the order they are called, one after another.
Creates a new ActionChains.
- Parameters:
driver (WebDriver) – The WebDriver instance which performs user actions.
duration (int) – override the default 250 msecs of DEFAULT_MOVE_DURATION in PointerInput
devices (list[PointerInput | KeyInput | WheelInput] | None) – Optional list of input devices (PointerInput, KeyInput, WheelInput) to use. If not provided, default devices will be created.
- click(on_element=None)[source]#
Clicks an element.
- Parameters:
on_element (WebElement | None) – The element to click. If None, clicks on current mouse position.
- Return type:
- click_and_hold(on_element=None)[source]#
Holds down the left mouse button on an element.
- Parameters:
on_element (WebElement | None) – The element to mouse down. If None, clicks on current mouse position.
- Return type:
- context_click(on_element=None)[source]#
Performs a context-click (right click) on an element.
- Parameters:
on_element (WebElement | None) – The element to context-click. If None, clicks on current mouse position.
- Return type:
- double_click(on_element=None)[source]#
Double-clicks an element.
- Parameters:
on_element (WebElement | None) – The element to double-click. If None, clicks on current mouse position.
- Return type:
- drag_and_drop(source, target)[source]#
Hold down the left mouse button on an element, then move to target and release.
- Parameters:
source (WebElement) – The element to mouse down.
target (WebElement) – The element to mouse up.
- Return type:
- drag_and_drop_by_offset(source, xoffset, yoffset)[source]#
Hold down the left mouse button on an element, then move by offset and release.
- Parameters:
source (WebElement) – The element to mouse down.
xoffset (int) – X offset to move to.
yoffset (int) – Y offset to move to.
- Return type:
- key_down(value, element=None)[source]#
Send a key press only without releasing it (modifier keys only).
- Parameters:
value (str) – The modifier key to send. Values are defined in Keys class.
element (WebElement | None) – The element to send keys. If None, sends a key to current focused element.
- Return type:
Example, pressing ctrl+c:
ActionChains(driver).key_down(Keys.CONTROL).send_keys("c").key_up(Keys.CONTROL).perform()
- key_up(value, element=None)[source]#
Releases a modifier key.
- Parameters:
value (str) – The modifier key to send. Values are defined in Keys class.
element (WebElement | None) – The element to send keys. If None, sends a key to current focused element.
- Return type:
Example, pressing ctrl+c:
ActionChains(driver).key_down(Keys.CONTROL).send_keys("c").key_up(Keys.CONTROL).perform()
- move_by_offset(xoffset, yoffset)[source]#
Moving the mouse to an offset from current mouse position.
- Parameters:
xoffset (int) – X offset to move to, as a positive or negative integer.
yoffset (int) – Y offset to move to, as a positive or negative integer.
- Return type:
- move_to_element(to_element)[source]#
Moving the mouse to the middle of an element.
- Parameters:
to_element (WebElement) – The WebElement to move to.
- Return type:
- move_to_element_with_offset(to_element, xoffset, yoffset)[source]#
Move the mouse to an element with the specified offsets.
Offsets are relative to the in-view center point of the element.
- Parameters:
to_element (WebElement) – The WebElement to move to.
xoffset (int) – X offset to move to, as a positive or negative integer.
yoffset (int) – Y offset to move to, as a positive or negative integer.
- Return type:
- pause(seconds)[source]#
Pause all inputs for the specified duration in seconds.
- Parameters:
seconds (float | int)
- Return type:
- release(on_element=None)[source]#
Releasing a held mouse button on an element.
- Parameters:
on_element (WebElement | None) – The element to mouse up. If None, releases on current mouse position.
- Return type:
- send_keys(*keys_to_send)[source]#
Sends keys to current focused element.
- Parameters:
keys_to_send (str) – The keys to send. Modifier keys constants can be found in the ‘Keys’ class.
- Return type:
- send_keys_to_element(element, *keys_to_send)[source]#
Sends keys to an element.
- Parameters:
element (WebElement) – The element to send keys.
keys_to_send (str) – The keys to send. Modifier keys constants can be found in the ‘Keys’ class.
- Return type:
- scroll_to_element(element)[source]#
Scroll the element into the viewport if it’s outside it.
Scrolls the bottom of the element to the bottom of the viewport.
- Parameters:
element (WebElement) – Which element to scroll into the viewport.
- Return type:
- scroll_by_amount(delta_x, delta_y)[source]#
Scroll by a provided amount with the origin in the top left corner.
Scrolls by provided amounts with the origin in the top left corner of the viewport.
- Parameters:
delta_x (int) – Distance along X axis to scroll using the wheel. A negative value scrolls left.
delta_y (int) – Distance along Y axis to scroll using the wheel. A negative value scrolls up.
- Return type:
- scroll_from_origin(scroll_origin, delta_x, delta_y)[source]#
Scroll by a provided amount based on a scroll origin (element or viewport).
The scroll origin is either the center of an element or the upper left of the viewport plus any offsets. If the origin is an element, and the element is not in the viewport, the bottom of the element will first be scrolled to the bottom of the viewport.
- Parameters:
scroll_origin (ScrollOrigin) – Where scroll originates (viewport or element center) plus provided offsets.
delta_x (int) – Distance along X axis to scroll using the wheel. A negative value scrolls left.
delta_y (int) – Distance along Y axis to scroll using the wheel. A negative value scrolls up.
- Raises:
MoveTargetOutOfBoundsException – If the origin with offset is outside the viewport.
- Return type: