Element Interface¶
Warning
This is auto-generated interface documentation for the abstract Element class.
This is essential for providing a comprehensive overview of all available methods for the Element object.
For the actual signature, please refer to mops.base.element.Element.
Note
All of these methods will work in the same way for Group class instances, which represent a collection of elements.
The Element class is a fundamental building block for creating Page Object Models (POM) in web testing.
It provides a comprehensive set of methods that allow you to interact with various web page elements which be found below.
- class mops.base.element.Element(*args: Any, **kwargs: Any)[source]¶
Represents a UI element that serves as a central component for interaction.
The
Elementclass is designed to be used withinPageorGroupobjects.It dynamically adapts to different driver types (Playwright, Appium, Selenium) and provides a unified interface for UI interactions.
- __init__(locator: Locator | str, name: str = '', parent: Group | Element | bool = None, wait: bool | None = None, driver_wrapper: DriverWrapper | Any = None)[source]¶
Initialize an Element based on the current driver.
If no driver is available, initialization is skipped and will be handled later in a Page or Group.
- Parameters:
locator (Union[Locator, str]) – The element’s locator. .LocatorType is optional.
name (str) – The name of the element, used for logging and identification purposes.
parent (Union[Group, Element, bool]) – The parent of the element. Provide
Falseto skip association.wait (Optional[bool]) – If True, the element will be checked in wait_page_loaded and is_page_opened methods of Page.
driver_wrapper (Union[DriverWrapper, Any]) – The
DriverWrapperinstance or an object containing it to be used for this element.
- property driver_wrapper: DriverWrapper¶
Retrieves the driver wrapper instance.
- Returns:
The current
DriverWrapperinstance that assigned for this object.- Return type:
- set_text(text: str, silent: bool = False) Element[source]¶
Clear the current input field and type the provided text.
- send_keyboard_action(action: str | KeyboardKeys) Element[source]¶
Send a keyboard action to the current element (e.g., press a key or shortcut).
- Parameters:
action (str or
KeyboardKeys) – The keyboard action to perform.- Returns:
- wait_visibility(*, timeout: int = 10, silent: bool = False, continuous: bool | float = False) Element[source]¶
- Wait until the element becomes visible.
Note: The method requires the use of named arguments.
A continuous visibility verification may be applied for given or default amount of time after the first condition is met.
Selenium:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- wait_visibility_without_error(*, timeout: float = 2.5, silent: bool = False, continuous: bool | float = False) Element[source]¶
- Wait for the element to become visible, without raising an error if it does not.
Note: The method requires the use of named arguments.
A continuous visibility verification may be applied for given or default amount of time after the first condition is met.
Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- Parameters:
- Returns:
- Wait until the element becomes hidden.
Note: The method requires the use of named arguments.
A continuous invisibility verification may be applied for given or default amount of time after the first condition is met.
Selenium:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- Wait for the element to become hidden, without raising an error if it does not.
Note: The method requires the use of named arguments.
A continuous invisibility verification may be applied for given or default amount of time after the first condition is met.
Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- Parameters:
- Returns:
- wait_availability(*, timeout: int = 10, silent: bool = False) Element[source]¶
- Wait until the element becomes available in DOM tree. n
Note: The method requires the use of named arguments.
Selenium:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- wait_for_text(expected_text: str | None = None, *, timeout: float = 10, silent: bool = False) Element[source]¶
Wait for the presence of a specific text in the current element, or for any non-empty text.
Note: The method requires the use of named arguments except
expected_text.Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- wait_for_value(expected_value: str | None = None, *, timeout: float = 10, silent: bool = False) Element[source]¶
Wait for a specific value in the current element, or for any non-empty value.
Note: The method requires the use of named arguments except
expected_value.Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- wait_enabled(*, timeout: float = 10, silent: bool = False) Element[source]¶
Wait for the element to become enabled and/or clickable.
Note: The method requires the use of named arguments.
Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- wait_disabled(*, timeout: float = 10, silent: bool = False) Element[source]¶
Wait for the element to become disabled.
Note: The method requires the use of named arguments.
Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- wait_for_size(expected_size: Size, *, timeout: float = 10, silent: bool = False) Element[source]¶
Wait until element size will be equal to given
SizeobjectNote: The method requires the use of named arguments except
expected_size.Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- wait_elements_count(expected_count: int, *, timeout: float = 10, silent: bool = False) Element[source]¶
Wait until the number of matching elements equals the expected count.
Note: The method requires the use of named arguments except
expected_count.Selenium & Playwright:
Applied
wait_condition()decorator integrates a 0.1 seconds delay for each iteration during the waiting process.
Appium:
Applied
wait_condition()decorator integrates an exponential delay (starting at 0.1 seconds, up to a maximum of 1.6 seconds) which increases with each iteration during the waiting process.
- property all_elements: list[mops.base.element.Element] | list[Any]¶
Return a list of all matching elements.
- Returns:
A list of wrapped
Elementobjects.
- is_visible(check_displaying: bool = True, silent: bool = False) bool[source]¶
Check if the current element’s top-left corner or bottom-right corner is visible on the screen.
- is_fully_visible(check_displaying: bool = True, silent: bool = False) bool[source]¶
Check is current element top left corner and bottom right corner visible on current screen
- scroll_into_view(block: ScrollTo = 'center', behavior: ScrollTypes = 'instant', sleep: float = 0, silent: bool = False) Element[source]¶
Scrolls the element into view using a JavaScript script.
- Parameters:
block (ScrollTo) – The scrolling block alignment. One of the
ScrollTooptions.behavior (ScrollTypes) – The scrolling behavior. One of the
ScrollTypesoptions.sleep (Union[int, float]) – Delay in seconds after scrolling. Can be an integer or a float.
- Returns:
- save_screenshot(file_name: str, screenshot_base: bytes | Image = None, convert_type: str | None = None) Image[source]¶
Save a screenshot of the element.
- Parameters:
file_name (str) – Path or filename for the screenshot.
screenshot_base (
bytes,PIL.Image.Image) – Screenshot binary or image to use (optional).convert_type (str) – Image conversion type before saving (optional).
- Returns:
- execute_script(script: str, *args: Any) Any[source]¶
Execute a JavaScript script on the element.
- Parameters:
script (str) – JavaScript code to be executed, referring to the element as
arguments[0].args (
typing.Any) – Any arguments to pass to the JavaScript.
- Returns:
typing.Anyresult from the script.
- assert_screenshot(filename: str = '', test_name: str = '', name_suffix: str = '', threshold: float | None = None, delay: float | None = None, scroll: bool = False, remove: Element | list[Element] = None, fill_background: str | bool = False, cut_box: Box = None, hide: Element | list[Element] = None) None[source]¶
Assert that the given screenshot matches the currently taken screenshot.
- Parameters:
filename (str) – The full name of the screenshot file. If empty - filename will be generated based on test name &
Elementnameargument & platform.test_name (str) – The custom test name for generated filename. If empty - it will be determined automatically.
name_suffix (str) – A suffix to add to the filename. Useful for distinguishing between positive and negative cases for the same
Elementduring one test.threshold (Optional[int, float]) – The acceptable threshold for comparing screenshots. If
None- takes default threshold or calculate its automatically based on screenshot size.delay (Optional[int, float]) – The delay in seconds before taking the screenshot. If
None- takes default delay.scroll (bool) – Whether to scroll to the element before taking the screenshot.
remove (Optional[Element or List[Element]]) –
Elementto remove from the screenshot. Can be a single element or a list of elements.fill_background (Optional[str or bool]) – The color to fill the background. If
True, uses a default color (black). If astr, uses the specified color.cut_box (Optional[Box]) – A
Boxspecifying a region to cut from the screenshot. IfNone, no region is cut.hide (Optional[Element or List[Element]]) –
Elementto hide in the screenshot. Can be a single element or a list of elements.
- Returns:
- soft_assert_screenshot(filename: str = '', test_name: str = '', name_suffix: str = '', threshold: float | None = None, delay: float | None = None, scroll: bool = False, remove: Element | list[Element] = None, fill_background: str | bool = False, cut_box: Box = None, hide: Element | list[Element] = None) tuple[bool, str][source]¶
Compare the currently taken screenshot to the expected screenshot and return a result.
- Parameters:
filename (str) – The full name of the screenshot file. If empty - filename will be generated based on test name &
Elementnameargument & platform.test_name (str) – The custom test name for generated filename. If empty - it will be determined automatically.
name_suffix (str) – A suffix to add to the filename. Useful for distinguishing between positive and negative cases for the same
Elementduring one test.threshold (Optional[int, float]) – The acceptable threshold for comparing screenshots. If
None- takes default threshold or calculate its automatically based on screenshot size.delay (Optional[int, float]) – The delay in seconds before taking the screenshot. If
None- takes default delay.scroll (bool) – Whether to scroll to the element before taking the screenshot.
remove (Optional[Element or List[Element]]) –
Elementto remove from the screenshot.fill_background (Optional[str or bool]) – The color to fill the background. If
True, uses a default color (black). If astr, uses the specified color.cut_box (Optional[Box]) – A
Boxspecifying a region to cut from the screenshot. IfNone, no region is cut.hide –
Elementto hide in the screenshot. Can be a single element or a list of elements.
- Returns:
- click(*, force_wait: bool = True, **kwargs: Any) Element¶
Clicks on the element.
Selenium/Appium:
Selenium Safari using js click instead.
- Parameters:
kwargs – compatibility arg for playwright
Playwright:
- Parameters:
kwargs – any kwargs params from source API
- Returns:
- click_in_alert() Element¶
Perform a click on an element inside an alert box (Mobile only). Note: Automatically switches to native context of the browser.
- Returns:
- click_outside(x: int = -5, y: int = -5) Element¶
Perform a click outside the current element, by default 5px left and above it.
- property driver: SeleniumWebDriver | AppiumWebDriver | PlaywrightSourcePage¶
Retrieves the source driver instance, which could be a Selenium, Appium, or Playwright driver.
- Returns:
Current source driver that assigned for this object, which is either n
selenium.webdriver.remote.webdriver.WebDriverornappium.webdriver.webdriver.WebDriverornplaywright.sync_api.Pageinstance.
- property element: SeleniumWebElement | AppiumWebElement | PlayWebElement¶
Returns a source element object, depending on the current driver in use.
- Returns:
selenium.webdriver.remote.webelement.WebElementornappium.webdriver.webelement.WebElementornplaywright.sync_api.Locator
- get_attribute(attribute: str, silent: bool = False) str¶
Retrieve a specific attribute from the current element.
- get_element_info(element: Element | None = None) str[source]¶
Retrieve detailed logging information for the specified element.
- get_rect() dict¶
Retrieve the size and position of the element as a dictionary.
- Returns:
dict- A dictionary {‘x’, ‘y’, ‘width’, ‘height’} of the element.
- hover_outside(x: int = 0, y: int = -5) Element¶
Hover the mouse outside the current element, by default 5px above it.
Check if the element is hidden.
- property location: Location¶
Get the location of the current element, including the x and y coordinates.
- Returns:
Location- An object representing the element’s position.
- log(message: str, level: str = 'info') None¶
Log a message with detailed context in the following format:
# Format [time][level][driver_index][module][function:line] <message> # Example [Aug 14][16:04:22.767][I][2_driver][play_element.py][is_displayed:328] Check visibility of "Mouse page"
- property screenshot_base: bytes¶
Return the binary screenshot data of the element.
- Returns:
bytes- screenshot binary
- screenshot_image(screenshot_base: bytes | None = None) Image¶
Return a
PIL.Image.Imageobject representing the screenshot of the web element. Appium iOS: Take driver screenshot and crop manually element from it
- property size: Size¶
Get the size of the current element, including width and height.
- Returns:
Size- An object representing the element’s dimensions.
- type_slowly(text: str, sleep_gap: float = 0.05, silent: bool = False) Element¶
Types text into the element slowly with a delay between keystrokes.