huskium.selenium package

Submodules

huskium.selenium.by module

class huskium.selenium.by.ByAttr

Bases: object

Mainly used for internal validation of By. It can also be used to check the available By attributes in your current Selenium and Appium version.

NAMES = ['CLASS_NAME', 'CSS_SELECTOR', 'ID', 'LINK_TEXT', 'NAME', 'PARTIAL_LINK_TEXT', 'TAG_NAME', 'XPATH', '_custom_finders', 'clear_custom_finders', 'get_finder', 'register_custom_finder']

A list of all By attribute names as strings.

OPTIONAL_VALUES = ['class name', 'css selector', 'id', 'link text', 'name', 'partial link text', 'tag name', 'xpath', {}, <bound method By.clear_custom_finders of <class 'selenium.webdriver.common.by.By'>>, <bound method By.get_finder of <class 'selenium.webdriver.common.by.By'>>, <bound method By.register_custom_finder of <class 'selenium.webdriver.common.by.By'>>, None]

A list of all By attribute values with None.

VALUES = ['class name', 'css selector', 'id', 'link text', 'name', 'partial link text', 'tag name', 'xpath', {}, <bound method By.clear_custom_finders of <class 'selenium.webdriver.common.by.By'>>, <bound method By.get_finder of <class 'selenium.webdriver.common.by.By'>>, <bound method By.register_custom_finder of <class 'selenium.webdriver.common.by.By'>>]

A list of all By attribute values.

huskium.selenium.ecex module

Expected Conditions Extension (ECEX)

Overview:

ECEX extends official expected_conditions (EC) module.

Locator Handling:

The locator follows the same structure as EC.

Index Feature:

The index allows using the find_elements(*locator)[index] pattern. If index is None, find_element(*locator) is used instead.

Exception Handling:

ECEX separates methods for locators and WebElements to enable more robust exception handling.

class huskium.selenium.ecex.ECEX

Bases: GenericECEX[WebDriver, WebElement]

class huskium.selenium.ecex.GenericECEX

Bases: Generic

static absence_of_all_elements_located(locator: tuple[str, str]) Callable[[WD], bool]

Checks Whether all elements CANNOT be found using the locator.

Parameters:

locator(by, value)

Returns:

True if all elements CANNOT be found, False otherwise.

Return type:

bool

static absence_of_element_located(locator: tuple[str, str], index: int | None) Callable[[WD], bool]

Checks Whether the element CANNOT be found using the locator and index.

Parameters:
  • locator(by, value)

  • indexNone for find_element(); int for find_elements()[index].

Returns:

True if the element CANNOT be found, False otherwise.

Return type:

bool

static element_located_to_be_clickable(locator: tuple[str, str], index: int | None) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be clickable using the locator and index.

Parameters:
  • locator(by, value)

  • indexNone for find_element(); int for find_elements()[index].

Returns:

WebElement if the found element is clickable, False otherwise.

Return type:

(WebElement | False)

Raises:
  • NoSuchElementException – Raised if the element cannot be found. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if the found element is stale. Optionally Ignored in WebDriverWait.

static element_located_to_be_selected(locator: tuple[str, str], index: int | None) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be selected using the locator and index.

Parameters:
  • locator(by, value)

  • indexNone for find_element(); int for find_elements()[index].

Returns:

WebElement if the found element is selected, False otherwise.

Return type:

(WebElement | False)

Raises:
  • NoSuchElementException – Raised if the element cannot be found. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if the found element is stale. Optionally Ignored in WebDriverWait.

static element_located_to_be_unclickable(locator: tuple[str, str], index: int | None, present: bool = True) Callable[[WD], WE | bool]

Checks Whether the element can be unclickable or absent using the locator and index.

Parameters:
  • locator(by, value)

  • indexNone for find_element(); int for find_elements()[index].

  • presentTrue for the element must be present; otherwise, it can be absent.

Returns:

WebElement` if the element is unclickable. If True, the element is absent and present is False. If False, the element is still clickable.

Return type:

(WebElement | bool)

Raises:
  • NoSuchElementException – Raised if the element is absent and present is True. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if the found element is stale and present is True. Optionally Ignored in WebDriverWait.

static element_located_to_be_unselected(locator: tuple[str, str], index: int | None) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be unselected using the locator and index.

Parameters:
  • locator(by, value)

  • indexNone for find_element(); int for find_elements()[index].

Returns:

WebElement` if the found element is unselected, False otherwise.

Return type:

(WebElement | False)

Raises:
  • NoSuchElementException – Raised if the element cannot be found. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if the found element is stale. Optionally Ignored in WebDriverWait.

static element_to_be_clickable(element: WE) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be clickable using the present element.

Parameters:

element (WebElement) – The present element.

Returns:

WebElement if the present element is clickable, False otherwise.

Return type:

(WebElement | False)

Raises:

StaleElementReferenceException – Raised if the present element is stale. Can be optionally caught and handled by relocating it using element_located_to_be_clickable() in an external process.

static element_to_be_selected(element: WE) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be selected using the present element.

Parameters:

element (WebElement) – The present element.

Returns:

WebElement if the present element is selected, False otherwise.

Return type:

(WebElement | False)

Raises:

StaleElementReferenceException – Raised if the present element is stale. Can be optionally caught and handled by relocating it using element_located_to_be_selected() in an external process.

static element_to_be_unclickable(element: WE, present: bool = True) Callable[[WD], WE | bool]

Checks Whether the element can be unclickable or absent using the present element.

Parameters:
  • element (WebElement) – The present element.

  • presentTrue for the element must be present; otherwise, it can be absent.

Returns:

WebElement if the element is unclickable. If True, the element is stale and present is False. If False, the element is still clickable.

Return type:

(WebElement | bool)

Raises:

StaleElementReferenceException – Raised if the found element is stale. Can be optionally caught and handled by relocating it using element_located_to_be_unclickable() in an external process.

static element_to_be_unselected(element: WE) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be unselected using the present element.

Parameters:

element – The present element.

Returns:

WebElement if the present element is unselected, False otherwise.

Return type:

(WebElement | False)

Raises:

StaleElementReferenceException – Raised if the present element is stale. Can be optionally caught and handled by relocating it using element_located_to_be_unselected() in an external process.

static invisibility_of_element(element: WE, present: bool = True) Callable[[WD], WE | bool]

Checks Whether the element can be invisible or absent using the present element.

Parameters:
  • element (WebElement) – The present element.

  • presentTrue for the element must be present; otherwise, it can be absent.

Returns:

WebElement if the element is invisible. If True, the element is stale and present is False. If False, the element is still visible.

Return type:

(WebElement | bool)

Raises:

StaleElementReferenceException – Raised if the found element is stale. Can be optionally caught and handled by relocating it using invisibility_of_element_located() in an external process.

static invisibility_of_element_located(locator: tuple[str, str], index: int | None, present: bool = True) Callable[[WD], WE | bool]

Checks Whether the element can be invisible or absent using the locator and index.

Parameters:
  • locator(by, value)

  • indexNone for find_element(); int for find_elements()[index].

  • presentTrue for the element must be present; otherwise, it can be absent.

Returns:

WebElement if the element is invisible. If True, the element is absent and present is False. If False, the element is still visible.

Return type:

(WebElement | bool)

Raises:
  • NoSuchElementException – Raised if the element is absent and present is True. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if the found element is stale and present is True. Optionally Ignored in WebDriverWait.

static presence_of_all_elements_located(locator: tuple[str, str]) Callable[[WD], list[WE]]

Checks whether at least one element can be found using the locator.

Parameters:

locator(by, value).

Returns:

The list of WebElement if found, or the empty list [] if not found.

Return type:

list[WebElement]

static presence_of_element_located(locator: tuple[str, str], index: int | None) Callable[[WD], WE]

Checks whether the element can be found using the locator and index.

Parameters:
  • locator(by, value).

  • indexNone for find_element(); int for find_elements()[index].

Returns:

The WebElement if found.

Return type:

WebElement

Raises:

NoSuchElementException – Raised if the element cannot be found. Ignored by default in WebDriverWait.

static visibility_of_all_elements_located(locator: tuple[str, str]) Callable[[WD], list[WE] | Literal[False]]

Checks Whether all elements can be visible using the locator.

Parameters:

locator (tuple) – (by, value)

Returns:

The list of WebElement if all elements are visible; otherwise, False if at least one element is invisible.

Return type:

(list[WebElement] | False)

Raises:
  • NoSuchElementException – Raised if any element cannot be found. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if any found element is stale. Optionally Ignored in WebDriverWait.

static visibility_of_any_elements_located(locator: tuple[str, str]) Callable[[WD], list[WE]]

Checks Whether at least one element can be visible using the locator.

Parameters:

locator (tuple) – (by, value)

Returns:

The list of WebElement if at least one element is visible; otherwise, the empty list [] if all elements are invisible.

Return type:

list[WebElement]

Raises:
  • NoSuchElementException – Raised if any elements cannot be found. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if any found element is stale. Optionally Ignored in WebDriverWait.

static visibility_of_element(element: WE) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be visible using the present element.

Parameters:

element – The present element.

Returns:

WebElement if the present element is visible, False otherwise.

Return type:

(WebElement | False)

Raises:

StaleElementReferenceException – Raised if the present element is stale. Can be optionally caught and handled by relocating it using visibility_of_element_located() in an external process.

static visibility_of_element_located(locator: tuple[str, str], index: int | None) Callable[[WD], WE | Literal[False]]

Checks Whether the element can be visible using the locator and index.

Parameters:
  • locator(by, value)

  • indexNone for find_element(); int for find_elements()[index].

Returns:

WebElement if the found element is visible, False otherwise.

Return type:

(WebElement | False)

Raises:
  • NoSuchElementException – Raised if the element cannot be found. Ignored by default in WebDriverWait.

  • StaleElementReferenceException – Raised if the found element is stale. Optionally Ignored in WebDriverWait.

huskium.selenium.element module

class huskium.selenium.element.Element(by: str | None = None, value: str | None = None, index: int | None = None, *, timeout: int | float | None = None, cache: bool | None = None, remark: str | None = None)

Bases: GenericElement[WebDriver, WebElement]

class huskium.selenium.element.GenericElement(by: str | None = None, value: str | None = None, index: int | None = None, *, timeout: int | float | None = None, cache: bool | None = None, remark: str | None = None)

Bases: Generic

property accessible_name: str

The ARIA Level of the current web element.

property action: ActionChains

The ActionChains instance used by the page.

property all_selected_options: list[WE]

Select API. Returns a list of all selected options belonging to this select tag.

property aria_role: str

The ARIA role of the current web element.

property border: dict[str, int]

The border of the element. For example: {‘left’: 150, ‘right’: 250, ‘top’: 200, ‘bottom’: 400}.

property by: str | None

The element locator strategy.

property cache: bool

The element final synced cache state.

property center: dict[str, int]

The center location of the element. For example: {‘x’: 80, ‘y’: 190}.

clear() Self

Clear the text of the field type element.

Examples

my_page.my_element.clear()
my_page.my_element.clear().send_keys('my text')
click() None

Click the element when it is clickable.

click_and_hold() Self

ActionChains API. Holds down the left mouse button on the current mouse position.

Examples

page.element.move_to_element().click_and_hold().perform()

# or
page.element.click_and_hold_on_element().perform()
click_and_hold_on_element() Self

ActionChains API. Moves the mouse to the element’s center and holds down the left mouse button it.

Examples

page.element.click_and_hold_on_element().perform()

# or
page.element.move_to_element().click_and_hold().perform()
property clickable_cache: WE | None

The clickable element cache, None otherwise.

property clickable_caching: WE

This attribute must be used with try-except.

Examples

try:
    self.clickable_caching.click()
except ELEMENT_REFERENCE_EXCEPTIONS:
    self.clickable_element.click()
property clickable_element: WE

The same as element.wait_clickable(reraise=True).

clicks() Self

ActionChains API. Clicks on the current mouse position.

Examples

page.element.move_to_element().clicks().perform()

# or
page.element.clicks_on_element().perform()
clicks_on_element() Self

ActionChains API. Moves the mouse to the element’s center and clicks it.

Examples

page.element.clicks_on_element().perform()

# or
page.element.move_to_element().clicks().perform()
context_click() Self

ActionChains API. Performs a context-click (right click) on the current mouse position.

Examples

page.element.move_to_element().context_click().perform()

# or
page.element.context_click_on_element().perform()
context_click_on_element() Self

ActionChains API. Moves the mouse to the element’s center and performs a context-click (right click) on it.

Examples

page.element.context_click_on_element().perform()

# or
page.element.move_to_element().context_click().perform()
classmethod default_cache() bool

Get the Element default cache value.

property default_remark: dict

The element default remark {"by": by, "value": value, "index": index}.

delayed_click(sleep: int | float = 0.5) None

Clicks the element after it becomes clickable, with a specified delay (sleep) in seconds.

deselect_all() None

Select API. Clear all selected entries. This is only valid when the SELECT supports multiple selections.

deselect_by_index(index: int) None

Select API. Deselect the option at the given index. This is done by examining the “index” attribute of an element, and not merely by counting.

Parameters:

index – The option at this index will be deselected.

deselect_by_value(value: str) None

Select API. Deselect all options that have a value matching the argument. That is, when given “foo” this would deselect an option like: <option value=”foo”>Bar</option>.

Parameters:

value – The value to match against.

deselect_by_visible_text(text: str) None

Select API. Deselect all options that display text matching the argument. That is, when given “Bar” this would deselect an option like: <option value=”foo”>Bar</option>.

Parameters:

text – The visible text to match against.

disable_cache() None

Disable the element cache.

classmethod disable_default_cache() None

Set default cache to False for all Element objects.

double_click() Self

ActionChains API. Double-clicks an element on the current mouse position.

Examples

page.element.move_to_element().double_click().perform()

# or
page.element.double_click_on_element().perform()
double_click_on_element() Self

ActionChains API. Moves the mouse to the element’s center and double-clicks it.

Examples

page.element.double_click_on_element().perform()

# or
page.element.move_to_element().double_click().perform()
drag_and_drop(target: Element) Self

ActionChains API. Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.

Examples

page.element1.drag_and_drop(page.element2).perform()
drag_and_drop_by_offset(xoffset: int, yoffset: int) Self

ActionChains API. Holds down the left mouse button on the source element, then moves to the target offset and releases the mouse button.

Parameters:
  • xoffset – X offset to move to, as a positive or negative integer.

  • yoffset – Y offset to move to, as a positive or negative integer.

Examples

page.element.drag_and_drop_by_offset(100, 200).perform()
property driver: WD

The WebDriver instance used by the page.

dynamic(by: str, value: str, index: int | None = None, *, timeout: int | float | None = None, cache: bool | None = None, remark: str | None = None) Self

Set dynamic elements as page.element.dynamic(…) pattern. All the args logic are the same as Element.

Examples

# my_page.py
class MyPage(Page):

    my_static_element = Element()

    def my_dynamic_element(self, id_):
        return self.my_static_element.dynamic(
            By.ID, id_, remark="dynamic_elem"
        )

# my_testcase.py
class MyTestCase:

    my_page = MyPage(driver)

    # The element ID is dynamic.
    id_ = Server.get_id()

    # Dynamically retrieve the element using any method.
    my_page.my_dynamic_element(id_).text

    # The static element can be used after the dynamic one set.
    my_page.my_static_element.click()
enable_cache() None

Enable the element cache.

classmethod enable_default_cache() None

Set default cache to True for all Element objects.

find_element() WE

Using the traditional find_element() or find_elements()[index] to locate element. It is recommended for use in situations where no waiting is required, such as the Android UiScrollable locator method.

property first_selected_option: WE

Select API. The first selected option in this select tag, or the currently selected option in a normal select.

get_attribute(name: str) str | dict | None

This method will first try to return the value of a property with the given name. If a property with that name doesn’t exist, it returns the value of the attribute with the same name. If there’s no attribute with that name, None is returned.

Values which are considered truthy, that is equals “true” or “false”, are returned as booleans. All other non-None values are returned as strings. For attributes or properties which do not exist, None is returned.

To obtain the exact value of the attribute or property, use selenium.webdriver.remote.BaseWebElement.get_dom_attribute or selenium.webdriver.remote.BaseWebElement.get_property.

Parameters:

name – Name of the attribute or property to retrieve.

Examples

# Check if the "active" CSS class is applied to an element.
is_active = "active" in target_element.get_attribute("class")
get_dom_attribute(name: str) str

Gets the given attribute of the element. Unlike selenium.webdriver.remote.BaseWebElement.get_attribute, this method only returns attributes declared in the element’s HTML markup.

Parameters:

name – Name of the attribute to retrieve.

Examples

text_length = element.get_dom_attribute("class")
get_property(name: Any) str | bool | dict | WE

Gets the given property of the element.

Parameters:

name – Name of the property to retrieve.

Examples

text_length = target_element.get_property("text_length")
property index: int | None

The element index.

is_clickable() bool

Whether the element is clickable (displayed and enabled).

is_enabled() bool

Whether the element is enabled.

is_present(timeout: int | float | None = None) bool

Whether the element is present within the timeout.

is_selected() bool

Whether the element is selected.

is_visible() bool

Whether the element is visible (displayed).

key_down(key: str) Self

ActionChains API. Sends only a modifier key press at the current focused position.

If you want to perform a combination key action, such as copying, it is recommended to use send_hotkey() instead.

Examples

# ctrl+a, ctrl+c
page.element.key_down_to_element(Key.CONTROL).sends_keys('a').key_up(Key.CONTROL)                .key_down(Key.CONTROL).sends_keys('c').key_up(Key.CONTROL).perform()

# or using send_hotkey()
page.element.send_hotkey_to_element(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c').perform()
key_down_to_element(key: str) Self

ActionChains API. Clicks the element’s center and sends a modifier key press only.

If you want to perform a combination key action, such as copying, it is recommended to use send_hotkey_to_element() instead.

Examples

# ctrl+a, ctrl+c
page.element.key_down_to_element(Key.CONTROL).sends_keys('a').key_up(Key.CONTROL)                .key_down(Key.CONTROL).sends_keys('c').key_up(Key.CONTROL).perform()

# or using send_hotkey()
page.element.send_hotkey_to_element(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c').perform()
key_up(key: str) Self

ActionChains API. Releases a modifier key at the current focused position.

If you want to perform a combination key action, such as copying, it is recommended to use send_hotkey() instead.

Examples

# ctrl+a, ctrl+c
page.element.key_down_to_element(Key.CONTROL).sends_keys('a').key_up(Key.CONTROL)                .key_down(Key.CONTROL).sends_keys('c').key_up(Key.CONTROL).perform()

# or using send_hotkey()
page.element.send_hotkey_to_element(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c').perform()
key_up_to_element(key: str) Self

ActionChains API. Clicks the element’s center and releases a modifier key.

If you want to perform a combination key action, such as copying, it is recommended to use send_hotkey_to_element() instead.

Examples

# ctrl+a, ctrl+c
page.element.key_down_to_element(Key.CONTROL).sends_keys('a').key_up(Key.CONTROL)                .key_down(Key.CONTROL).sends_keys('c').key_up(Key.CONTROL).perform()

# or using send_hotkey()
page.element.send_hotkey_to_element(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c').perform()
property location: dict

The location of the element when it is in the renderable canvas. For example: {‘x’: 200, ‘y’: 300}.

property location_once_scrolled_into_view: dict

THIS PROPERTY MAY CHANGE WITHOUT WARNING.

Use this to determine the on-screen location of an element that can be clicked, and it scrolls the element into view if necessary.

Returns the top-left corner coordinates on the screen, or (0, 0) if the element is not visible.

property locator: tuple[str, str]

The element locator (by, value)

property logger: PageElementLoggerAdapter

The element logger.

move_by_offset(xoffset: int, yoffset: int) Self

ActionChains API. Moving the mouse to an offset from current mouse position.

Parameters:
  • xoffset – X offset to move to, as a positive or negative integer.

  • yoffset – Y offset to move to, as a positive or negative integer.

Examples

page.element.move_to_element().move_by_offset(100, 200).perform()
move_to_element() Self

ActionChains API. Moving the mouse to the middle of an element.

Examples

page.element.scroll_to_element().move_to_element().perform()
move_to_element_with_offset(xoffset: int, yoffset: int) Self

ActionChains API. Move the mouse by an offset of the specified element. Offsets are relative to the in-view center point of the element.

Parameters:
  • xoffset – X offset to move to, as a positive or negative integer.

  • yoffset – Y offset to move to, as a positive or negative integer.

Examples

page.element.scroll_to_element().move_to_element_with_offset(100, 200).perform()
property options: list[WE]

Select API. Returns a list of all options belonging to this select tag.

property page: GenericPage[WD, WE]

The Page instance from the descriptor.

pause(seconds: int | float) Self

ActionChains API. Pause all inputs for the specified duration in seconds.

perform() None

ActionChains API. Performs all stored actions.

Examples

# Basic usage. Execute element actions.
page.element.scroll_to_element().clicks().perform()

# Multiple actions to call, set perform to the last action.
# This will execute all actions in page not just page.element2.
page.element1.scroll_to_element().clicks()
page.element2.drag_and_drop(page.element3).perform()

# As above, it is the same to call perform by page:
page.element1.scroll_to_element().clicks()
page.element2.drag_and_drop(page.element3)
page.perform()
property present_cache: WE | None

The present element cache, None otherwise.

property present_caching: WE

This attribute must be used with try-except.

Examples

try:
    self.present_caching.text
except ELEMENT_REFERENCE_EXCEPTIONS:
    self.present_element.text
property present_element: WE

The same as element.wait_present(reraise=True).

property rect: dict

The location and size of the element. For example: {‘x’: 10, ‘y’: 15, ‘width’: 100, ‘height’: 200}.

release() Self

ActionChains API. Releasing a held mouse button on the current position.

Examples

page.element.click_and_hold_on_element().release().perform()
release_on_element() Self

ActionChains API. Releasing a held mouse button on an element.

Examples

page.element.release_on_element().perform()
property remark: str | dict

The element remark. If not set, defaults to {"by": by, "value": value, "index": index}.

reset_actions() None

ActionChains API. Clears actions that are already stored in object of Page. once called, it will reset all stored actions in page.

Examples

# Reset the stored actions by the last reset_actions.
page.element1.scroll_to_element().clicks()
page.element2.click_and_hold().reset_actions()

# There is a better one structure,
# reset all action calls made by page.
page.element1.scroll_to_element().clicks()
page.element2.click_and_hold()
page.reset_actions()
reset_remark(value: str | None = None) None

Reset the element remark. If value is None, defaults to {"by": by, "value": value, "index": index}.

reset_timeout(value: int | float | None = None) None

Reset the element timeout in seconds.

screenshot(filename: str) bool

Saves a screenshot of the current element to a PNG image file. Returns False if there is any IOError, else returns True.

Parameters:

filename – The full path you wish to save your screenshot to. This should end with a .png extension.

scroll_by_amount(delta_x: int, delta_y: int) Self

ActionChains API. Scrolls by provided amounts with the origin in the top left corner of the viewport.

Parameters:
  • delta_x – Distance along X axis to scroll using the wheel. A negative value scrolls left.

  • delta_y – Distance along Y axis to scroll using the wheel. A negative value scrolls up.

Examples

page.element.move_to_element().scroll_by_amount(100, 200).perform()
scroll_from_element(x_offset: int = 0, y_offset: int = 0, delta_x: int = 0, delta_y: int = 0) Self

ActionChains API.

Set the origin to the center of the element with an offset, and perform the swipe with the specified delta.

If the element is not in the viewport, the bottom of the element will first be scrolled to the bottom of the viewport.

Parameters:
  • x_offset – From origin element center, a negative value offset left.

  • y_offset – From origin element center, a negative value offset up.

  • delta_x – Distance along X axis to scroll using the wheel, a negative value scrolls left.

  • delta_y – Distance along Y axis to scroll using the wheel, a negative value scrolls up.

Examples

page.element.scroll_from_element(-30, -50, 150, 100).clicks().perform()
scroll_from_origin(x_offset: int = 0, y_offset: int = 0, delta_x: int = 0, delta_y: int = 0) Self

ActionChains API. Scrolls by provided amount based on a provided origin. The scroll origin is the upper left of the viewport plus any offsets.

Parameters:
  • x_offset – from origin viewport, a negative value offset left.

  • y_offset – from origin viewport, a negative value offset up.

  • delta_x – Distance along X axis to scroll using the wheel. A negative value scrolls left.

  • delta_y – 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.

Examples

page.element.scroll_to_element().scroll_from_origin(150, 100, 100, 200).perform()
scroll_to_element() Self

ActionChains API. If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.

Examples

page.element.scroll_to_element().clicks().perform()
property select: Select

The Select instance used by the present element.

select_by_index(index: int) None

Select API. Select the option at the given index.

This is done by examining the “index” attribute of an element, and not merely by counting.

Parameters:

index – The option at this index will be selected, throws NoSuchElementException if there is no option with specified index in SELECT.

select_by_value(value: str) None

Select API. Select all options that have a value matching the argument.

That is, when given “foo” this would select an option like: <option value=”foo”>Bar</option>

Parameters:

value – The value to match against.

select_by_visible_text(text: str) None

Select API. Select all options that display text matching the argument.

That is, when given “Bar” this would select an option like: <option value=”foo”>Bar</option>

Parameters:

text – The visible text to match against, throws NoSuchElementException if there is no option with specified text in SELECT.

property select_cache: Select | None

The Select instance, None otherwise.

property select_caching: Select

This attribute must be used with try-except.

Examples

try:
    self.select_caching.options
except ELEMENT_REFERENCE_EXCEPTIONS:
    self.select.options
send_hotkey(*keys: str) Self

ActionChains API. Sends the hotkey sequence to the focused position.

Examples

# Ensure that it is already at the target position or element.
page.element1.send_hotkey_to_element(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c')
page.element2.send_hotkey_to_element(Keys.CONTROL, 'v').perform()
send_hotkey_to_element(*keys: str) Self

ActionChains API. Clicks the element’s center and sends the hotkey sequence to it.

Examples

page.element1.send_hotkey_to_element(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c')
page.element2.send_hotkey_to_element(Keys.CONTROL, 'v').perform()
send_keys(*value) Self

Simulates typing into the element.

Parameters:

*value – The texts or keys to typing.

Examples

my_page.my_element.send_keys('my_text')
my_page.my_element.clear().send_keys('my_text')
my_page.my_element.click().clear().send_keys('my_text')
sends_keys(*keys: str) Self

ActionChains API. Sends keys to the current focused position.

Examples

# Combine with key_down() and key_up()
page.element.key_down_to_element(Key.CONTROL).sends_keys('a').key_up(Key.CONTROL).perform()
sends_keys_to_element(*keys: str) Self

ActionChains API. Sends keys to an element.

Examples

page.element.sends_keys_to_element('some text', Keys.ENTER)
property shadow_root: ShadowRoot

Returns a ShadowRoot object of the element if there is one or an error. Only works from Chromium 96, Firefox 96, and Safari 16.4 onwards. If no shadow root was attached, raises NoSuchShadowRoot.

property size: dict

The size of the element. For example: {‘width’: 200, ‘height’: 100}.

submit() None

Submits a form.

switch_to_frame(timeout: int | float | None = None, reraise: bool | None = None) bool

Switch to the frame if is available.

property tag_name: str

The tagName property.

property text: str

The text of the element when it is present.

property timeout: int | float

The element current wait timeout in seconds.

unset_cache() None

Unset the element cache to follow the default cache state.

property value: str | None

The element locator value.

value_of_css_property(property_name: Any) str

The value of a CSS property.

Examples

page.element.value_of_css_property('color')
property visible_cache: WE | None

The visible element cache, None otherwise.

property visible_caching: WE

This attribute must be used with try-except.

Examples

try:
    self.visible_caching.text
except ELEMENT_REFERENCE_EXCEPTIONS:
    self.visible_element.text
property visible_element: WE

The same as element.wait_visible(reraise=True).

property visible_text: str

The text of the element when it is visible.

visible_value_of_css_property(property_name: Any) str

The visible value of a CSS property.

Examples

page.element.visible_value_of_css_property('color')
property wait: Wait

The current WebDriverWait instance.

wait_absent(timeout: int | float | None = None, reraise: bool | None = None) bool

Waits for the element to become absent.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

True if absent within the timeout; False if remains present after the timeout(reraise=False).

Return type:

bool

Raises:

TimeoutException – Raised if it remains present after the timeout(reraise=True).

wait_clickable(timeout: int | float | None = None, reraise: bool | None = None) WE | Literal[False]

Waits for the element to become clickable.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The WebElement if clickable within the timeout; False if remains unclickable or absent after the timeout(reraise=False).

Return type:

(WebElement | False)

Raises:

TimeoutException – Raised if it remains unclickable or absent after the timeout(reraise=True).

wait_invisible(timeout: int | float | None = None, present: bool = True, reraise: bool | None = None) WE | bool

Waits for the element to become invisible (or absent).

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • present – Specifies whether the element must be present. If True, the element must be present. If False, the element can be absent.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The WebElement if invisible within the timeout; True if absent(present=False) within the timeout; False if remains visible after the timeout(reraise=False).

Return type:

(WebElement | bool)

Raises:

TimeoutException – Raised if it remains visible after the timeout(reraise=True).

wait_present(timeout: int | float | None = None, reraise: bool | None = None) WE | Literal[False]

Waits for the element to become present.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The WebElement if present within the timeout; False if remains absent after the timeout(reraise=False).

Return type:

(WebElement | False)

Raises:

TimeoutException – Raised if it remains absent after the timeout(reraise=True).

wait_selected(timeout: int | float | None = None, reraise: bool | None = None) WE | Literal[False]

Waits for the element to become selected.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The WebElement if selected within the timeout; False if remains unselected or absent after the timeout(reraise=False).

Return type:

(WebElement | False)

Raises:

TimeoutException – Raised if it remains unselected or absent after the timeout(reraise=True).

wait_unclickable(timeout: int | float | None = None, present: bool = True, reraise: bool | None = None) WE | bool

Waits for the element to become unclickable (or absent).

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • present – Specifies whether the element must be present. If True, the element must be present. If False, the element can be absent.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The WebElement if unclickable within the timeout; True if absent(present=False) within the timeout; False if remains clickable after the timeout(reraise=False).

Return type:

(WebElement | bool)

Raises:

TimeoutException – Raised if it remains clickable after the timeout(reraise=True).

wait_unselected(timeout: int | float | None = None, reraise: bool | None = None) WE | Literal[False]

Waits for the element to become unselected.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The WebElement if unselected within the timeout; False if remains selected or absent after the timeout(reraise=False).

Return type:

(WebElement | False)

Raises:

TimeoutException – Raised if it remains selected or absent after the timeout(reraise=True).

wait_visible(timeout: int | float | None = None, reraise: bool | None = None) WE | Literal[False]

Waits for the element to become visible.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The WebElement if visible within the timeout; False if remains invisible or absent after the timeout(reraise=False).

Return type:

(WebElement | False)

Raises:

TimeoutException – Raised if it remains invisible or absent after the timeout(reraise=True).

waiting(timeout: int | float | None = None, ignored_exceptions: Type[Exception] | Iterable[Type[Exception]] | None = None) Wait

The final WebDriverWait instance.

huskium.selenium.elements module

class huskium.selenium.elements.Elements(by: str | None = None, value: str | None = None, *, timeout: int | float | None = None, remark: str | None = None)

Bases: GenericElements[WebDriver, WebElement]

class huskium.selenium.elements.GenericElements(by: str | None = None, value: str | None = None, *, timeout: int | float | None = None, remark: str | None = None)

Bases: Generic

property accessible_names: list[str]

The ARIA Levels of the current webelement.

property all_present_elements: list[WE]

The same as elements.wait_all_present(reraise=True).

property all_visible_elements: list[WE]

The same as elements.wait_all_visible(reraise=True).

property all_visible_texts: list[str]

The texts of all elements until they are visible.

property any_visible_elements: list[WE]

The same as elements.wait_any_visible(reraise=True).

property any_visible_texts: list[str]

The texts of the elements if at least one is visible.

are_all_present(timeout: int | float | None = None) bool

Whether the all elements are present.

Parameters:

timeout – Maximum wait time in seconds.

Returns:

True if all are present within the timeout, False otherwise.

Return type:

bool

are_all_visible() bool

Whether all elements are visible.

Returns:

True if all are visible within the timeout, False otherwise.

Return type:

bool

are_any_visible() bool

Whether at least one element is visible.

Returns:

True if at least one is visible within the timeout, False otherwise.

Return type:

bool

property aria_roles: list[str]

The ARIA roles of the current web elements.

property by: str | None

The elements locator strategy.

property centers: list[dict]

The center locations of all present elements.

property default_remark: dict

The elements default remark {"by": by, "value": value}.

property driver: WD

The WebDriver instance used by the page.

dynamic(by: str, value: str, *, timeout: int | float | None = None, remark: str | None = None) Self

Set dynamic elements as page.elements.dynamic(…) pattern. All the args logic are the same as Elements.

Examples

# my_page.py
class MyPage(Page):

    my_static_elements = Elements()

    def my_dynamic_elements(self, accid):
        return self.my_static_elements.dynamic(
            By.ACCESSIBILITY_ID, accid,
            remark="Dynamically set my_static_elements."
        )

# my_testcase.py
class MyTestCase:
    ...
    my_page = MyPage(driver)
    # The element accessibility id is dynamic.
    id_ = Server.get_id()
    # Dynamically retrieve the elements using any method.
    my_page.my_dynamic_elements(id_).texts
    # The static elements can be used after the dynamic one is set.
    my_page.my_static_elements.locations
find(index: int | None = None, timeout: int | float | None = None, reraise: bool | None = None) list[WE] | WE | Literal[False]

Waits for the element or elements to be present.

Parameters:
  • indexNone for all elemets.

  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

The list[WebElement] for index=None; the WebElement for index=int; False if no any element.

Return type:

(list[WebElement] | WebElement | False)

find_elements(index: int | None = None) list[WE] | WE

Using the traditional find_elements() or find_elements()[index] (if there is index) to locate elements. If there are no any element found, it will return empty list [].

get_attributes(name: str) list[str | dict | None]

The specific attributes or properties of all present elements.

get_dom_attributes(name: str) list[str]

Gets the given attributes of all present elements. Unlike selenium.webdriver.remote.BaseWebElement.get_attribute, this method only returns attributes declared in the element’s HTML markup.

Parameters:

name – Name of the attribute to retrieve.

Examples

text_length = page.element.get_dom_attributes("class")
get_properties(name: str) list[str | bool | dict | WE]

The specific properties of all present elements.

property locations: list[dict[str, int]]

The locations of all present elements.

property locator: tuple[str, str]

The elements locator (by, value)

property logger: PageElementLoggerAdapter

The elements logger.

property page: GenericPage[WD, WE]

The Page instance from the descriptor.

property quantity: int

The quantity of all present elements.

property rects: list[dict[str, int]]

The rects of all present elements.

property remark: str | dict

The elements remark. If not set, defaults to {"by": by, "value": value}.

reset_remark(value: str | None = None) None

Reset the elements remark. If value is None, defaults to {"by": by, "value": value}.

reset_timeout(value: int | float | None = None) None

Reset the elements timeout in seconds.

property shadow_roots: list[ShadowRoot]

Returns shadow roots of the elements if there is one or an error. Only works from Chromium 96, Firefox 96, and Safari 16.4 onwards. If no shadow root was attached, raises NoSuchShadowRoot.

property sizes: list[dict]

The sizes of all present elements.

property texts: list[str]

The texts of all present elements.

property timeout: int | float

The elements current wait timeout in seconds.

property value: str | None

The elements locator value.

property wait: Wait

The current WebDriverWait instance.

wait_all_absent(timeout: int | float | None = None, reraise: bool | None = None) bool

Waits for all elements to become absent.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

True if all are absent within the timeout. False if at least one is present after the timeout(reraise=False).

Return type:

bool

Raises:

TimeoutException – Raised if at least one remain present after the timeout(reraise=True).

wait_all_present(timeout: int | float | None = None, reraise: bool | None = None) list[WE] | Literal[False]

Waits for all elements to become present.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

A list of WebElement if all are present within the timeout. False if all remain absent after the timeout(reraise=False).

Return type:

(list[WebElement] | False)

Raises:

TimeoutException – Raised if all remain absent after the timeout(reraise=True).

wait_all_visible(timeout: int | float | None = None, reraise: bool | None = None) list[WE] | Literal[False]

Waits for all elements to become visible.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

A list of WebElement if all are visible within the timeout. False if at least one remain invisible or absent after the timeout(reraise=False).

Return type:

(list[WebElement] | False)

Raises:

TimeoutException – Raised if at least one remain invisible or absent after the timeout(reraise=True).

wait_any_visible(timeout: int | float | None = None, reraise: bool | None = None) list[WE] | Literal[False]

Waits for at least one element to become visible.

Parameters:
  • timeout – Maximum wait time in seconds. If None, uses element.timeout. If set, overrides with this value.

  • reraise – Defines behavior when timed out. If None, follows page.reraise. If True, raises TimeoutException; if False, returns False.

Returns:

A list of WebElement if at least one is visible within the timeout. False if all remain invisible or absent after the timeout(reraise=False).

Return type:

(list[WebElement] | False)

Raises:

TimeoutException – Raised if all remain invisible or absent after the timeout(reraise=True).

waiting(timeout: int | float | None = None, ignored_exceptions: Type[Exception] | Iterable[Type[Exception]] | None = None) Wait

The final WebDriverWait instance.

huskium.selenium.page module

class huskium.selenium.page.GenericPage(driver: WD, timeout: int | float = 10, reraise: bool = True, remark: str = 'Page')

Bases: Generic

accept_alert() None

Accept an alert.

property action: ActionChains

The ActionChains instance.

Adds a cookie to your current session.

Parameters:

cookie – A dictionary object. Required keys: “name” and “value”; optional keys: “path”, “domain”, “secure”, “httpOnly”, “expiry”, “sameSite”.

Examples

page.add_cookie({'name': 'foo', 'value': 'bar'})
page.add_cookie({'name': 'foo', 'value': 'bar', 'path': '/'})
page.add_cookie({'name': 'foo', 'value': 'bar', 'path': '/', 'secure': True})
page.add_cookie({'name': 'foo', 'value': 'bar', 'sameSite': 'Strict'})
add_cookies(cookies: list[dict]) None

Adds cookies to your current session.

Parameters:

cookies – list[dict]

Examples

cookies = [
    {'name' : 'foo', 'value' : 'bar'},
    {'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure' : True}},
    ...
]
page.add_cookies(cookies)
property alert_text: str

Gets the text of the Alert.

back() None

Goes one step backward in the browser history.

click() Self

ActionChains API. Clicks on current mouse position.

Examples

page.move_by_offset(100, 200).click().perform()
click_and_hold() Self

ActionChains API. Holds down the left mouse button on current mouse position.

Examples

page.move_by_offset().click_and_hold().perform()
close() None

Closes the current window.

context_click() Self

ActionChains API. Performs a context-click (right click) on current mouse position.

Examples

page.move_by_offset().context_click().perform()
property current_window_handle: str

The handle of the current window.

delete_all_cookies() None

Delete all cookies in the scope of the session.

Examples

self.delete_all_cookies()

Deletes a single cookie with the given name.

delete_downloadable_files() None

Deletes all downloadable files.

property dialog: Dialog

Returns the FedCM dialog object for interaction.

dismiss_alert() None

Dismisses an alert.

double_click() Self

ActionChains API. Double-clicks on current mouse position.

Examples

page.move_by_offset().double_click().perform()
download_file(file_name: str, target_directory: str) None

Downloads a file with the specified file name to the target directory.

Parameters:
  • file_name – The name of the file to download.

  • target_directory – The path to the directory to save the downloaded file.

property driver: WD

The WebDriver instance.

execute_async_script(script: str, *args) Any

Asynchronously Executes JavaScript in the current window/frame.

Parameters:
  • script – The JavaScript to execute.

  • *args – Any applicable arguments for your JavaScript.

Example:

script = (
    "var callback = arguments[arguments.length - 1]; "
    "window.setTimeout(function(){ callback('timeout') }, 3000);"
)
page.execute_async_script(script)
execute_script(script: str, *args) Any

Synchronously Executes JavaScript in the current window or frame.

Parameters:
  • script – The JavaScript to execute.

  • *args – Any applicable arguments for your JavaScript.

Examples

page.execute_script('return document.title;')
property fedcm: FedCM

Returns an object providing access to all Federated Credential Management (FedCM) dialog commands.

Examples

title = page.fedcm.title
subtitle = page.fedcm.subtitle
dialog_type = page.fedcm.dialog_type
accounts = page.fedcm.account_list
page.fedcm.select_account(0)
page.fedcm.accept()
page.fedcm.dismiss()
page.fedcm.enable_delay()
page.fedcm.disable_delay()
page.fedcm.reset_cooldown()
fedcm_dialog(timeout: float = 5, poll_frequency: float = 0.5, ignored_exceptions: Iterable[Type[Exception]] | None = None) Dialog | None

Waits for and returns the FedCM dialog.

Parameters:
  • timeout – How long to wait for the dialog.

  • poll_frequency – How frequently to poll.

  • ignored_exceptions – Exceptions to ignore while waiting.

Returns:

The FedCM dialog object if found.

Return type:

Dialog

Raises:
  • TimeoutException – If dialog doesn’t appear.

  • WebDriverException – If FedCM not supported.

forward() None

Goes one step forward in the browser history.

fullscreen_window() None

Invokes the window manager-specific ‘full screen’ operation.

get(url: str) None

Loads a web page in the current browser session.

Get a single cookie by name. Returns the cookie if found, None if not.

get_cookies() list[dict]

Returns a set of dictionaries, corresponding to cookies visible in the current session.

get_downloadable_files() list

Retrieves the downloadable files as a map of file names and their corresponding URLs.

get_log(log_type: Any) Any

Gets the log for a given log type.

Parameters:

log_type – Type of log that which will be returned.

Examples

page.get_log('browser')
page.get_log('driver')
page.get_log('client')
page.get_log('server')
get_window_border() dict[str, int]

window border: {‘left’: int, ‘right’: int, ‘top’: int, ‘bottom’: int}

get_window_center() dict[str, int]

window center: {‘x’: int, ‘y’: int}

get_window_position(windowHandle: str = 'current') dict

Gets the (x, y) coordinates of the window. For example: {‘x’: 0, ‘y’: 0}.

get_window_rect() dict

Gets the x, y coordinates of the window as well as height and width of the current window. For example: {‘x’: 0, ‘y’: 0, ‘width’: 500, ‘height’: 250}.

get_window_size(windowHandle: str = 'current') dict

Gets the width and height of the current window. For example: {‘width’: 430, ‘height’: 920}.

implicitly_wait(timeout: int | float = 30) None

implicitly wait

key_down(key: str) Self

ActionChains API. Sends a modifier key press only to the page, without releasing it.

If you want to perform a combination key action, such as copying, it is recommended to use send_hotkey() instead.

Examples

# ctrl+a, ctrl+c
page.key_down(Key.CONTROL).send_keys('a').key_up(Key.CONTROL)                .key_down(Key.CONTROL).send_keys('c').key_up(Key.CONTROL).perform()

# or using send_hotkey()
page.send_hotkey(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c').perform()
key_up(key: str) Self

ActionChains API. Releases a modifier key on a page.

If you want to perform a combination key action, such as copying, it is recommended to use send_hotkey() instead.

Examples

# ctrl+a, ctrl+c
page.key_down(Key.CONTROL).send_keys('a').key_up(Key.CONTROL)                .key_down(Key.CONTROL).send_keys('c').key_up(Key.CONTROL).perform()

# or using send_hotkey()
page.send_hotkey(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c').perform()
property list_pinned_scripts: list[str]

Get listed pinned scripts from storage.

property log_types: Any

Gets a list of the available log types. This only works with w3c compliant browsers.

property logger: PageElementLoggerAdapter

The page logger.

maximize_window() None

Maximizes the current window that webdriver is using.

minimize_window() None

Invokes the window manager-specific ‘minimize’ operation.

property mobile: Mobile

Return Mobile object.

move_by_offset(xoffset: int, yoffset: int) Self

ActionChains API. Moving the mouse to an offset from current mouse position.

Parameters:
  • xoffset – X offset to move to, as a positive or negative integer.

  • yoffset – Y offset to move to, as a positive or negative integer.

Examples

page.move_by_offset(100, 200).perform()
property name: str

Returns the name of the underlying browser for this instance.

new_window_is_opened(current_handles: list[str], timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for the number of windows to be a certain value.

number_of_windows_to_be(num_windows: int, timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for the number of windows to be a certain value.

pause(seconds: int | float) Self

ActionChains API. Pause all inputs for the specified duration in seconds.

perform() None

ActionChains API. Performs all stored actions. Once called, it will execute all stored actions in page.

Examples

# Perform all saved actions:
page.element1.scroll_to_element().clicks()
page.element2.drag_and_drop(page.element3)
page.perform()
pin_script(script: str, script_key: Any | None = None) ScriptKey

Store common javascript scripts to be executed later by a unique hashable ID.

property pinned_scripts: dict

Get pinned scripts as dict from storage.

print_page(print_options: PrintOptions | None = None) str

Takes PDF of the current page.

quit() None

Quits the driver and closes every associated window.

refresh() None

Refreshes the current page.

release() Self

ActionChains API. Releasing a held mouse button on current mouse position.

Examples

page.click_and_hold().release().perform()
property remark: str

The page remark.

property reraise: bool

The timeout reraise state.

reset_actions() None

ActionChains API. Clears actions that are already stored in object of Page. Once called, it will reset all stored actions in page.

Examples

# Reset all saved actions:
page.element1.scroll_to_element().clicks()
page.element2.drag_and_drop(page.element3)
page.reset_actions()
save_screenshot(filename: Any) bool

Saves a screenshot of the current window to a PNG image file. Returns False if there is any IOError, else returns True. Use full paths in your filename.

Parameters:

filename – The full path you wish to save your screenshot to. This should end with a .png extension.

Examples

driver.save_screenshot('/Screenshots/foo.png')
scroll_by_amount(delta_x: int, delta_y: int) Self

ActionChains API. Scrolls by provided amounts with the origin in the top left corner of the viewport.

Parameters:
  • delta_x – Distance along X axis to scroll using the wheel. A negative value scrolls left.

  • delta_y – Distance along Y axis to scroll using the wheel. A negative value scrolls up.

Examples

page.scroll_by_amount(100, 200).perform()
scroll_from_origin(x_offset: int = 0, y_offset: int = 0, delta_x: int = 0, delta_y: int = 0) Self

ActionChains API. Scrolls by provided amount based on a provided origin. The scroll origin is the upper left of the viewport plus any offsets.

Parameters:
  • x_offset – from origin viewport, a negative value offset left.

  • y_offset – from origin viewport, a negative value offset up.

  • delta_x – Distance along X axis to scroll using the wheel. A negative value scrolls left.

  • delta_y – 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.

Examples

page.scroll_from_origin(150, 100, 100, 200).perform()
send_hotkey(*keys: str) Self

ActionChains API. Sends hotkey to the page.

Examples

# ctrl+a, ctrl+c
page.send_hotkey(Key.CONTROL, 'a').send_hotkey(Keys.CONTROL, 'c').perform()
send_keys(*keys: str) Self

ActionChains API. Sends keys to current focused position.

Examples

# combine with key_down and key_up
page.key_down(Key.CONTROL).send_keys('a').key_up(Key.CONTROL)
set_page_load_timeout(time_to_wait: int | float) None

Set the amount of time to wait for a page load to complete before throwing an error.

Examples

page.set_page_load_timeout(30)
set_script_timeout(time_to_wait: int | float) None

Set the amount of time that the script should wait during an execute_async_script call before throwing an error.

Examples

page.set_script_timeout(30)
set_window_position(x: int = 0, y: int = 0, windowHandle: str = 'current') dict

Sets the (x, y) position of the current window. (window.moveTo)

set_window_rect(x: int | None = None, y: int | None = None, width: int | None = None, height: int | None = None) dict | None

Sets the x, y coordinates of the window as well as height and width of the current window. This method is only supported for W3C compatible browsers; other browsers should use set_window_position and set_window_size.

Examples

page.set_window_rect(x=10, y=10)
page.set_window_rect(width=100, height=200)
page.set_window_rect(x=10, y=10, width=100, height=200)
set_window_size(width: int | None = None, height: int | None = None, windowHandle: str = 'current') None

Sets the width and height of the current window.

property source: str

The source of the current page.

property supports_fedcm: bool

Returns whether the browser supports FedCM capabilities.

switch_to_active_element() WE

Returns the element with focus, or BODY if nothing has focus.

switch_to_alert(timeout: int | float | None = None, reraise: bool | None = None) Alert | Literal[False]

Switch to alert.

switch_to_default_content() None

Switch focus to the default frame.

switch_to_frame(reference: str | int) None

Switches focus to the specified frame by name or index. If you want to switch to an iframe WebElement, use xxx_page.my_element.switch_to_frame() instead.

Parameters:

reference – The name of the window to switch to, or an integer representing the index.

Examples

xxx_page.switch_to_frame('name')
xxx_page.switch_to_frame(1)
switch_to_new_window(type_hint: str | None) None

Switches to a new top-level browsing context. The type hint can be one of “tab” or “window”. If not specified the browser will automatically select it.

switch_to_parent_frame() None

Switches focus to the parent context. If the current context is the top level browsing context, the context remains unchanged.

switch_to_window(window: str | int = 0) None

Switches focus to the specified window.

Parameters:

windowstr for Window name; int for Window index.

Examples

page.switch_to_window('main')
page.switch_to_window(1)
property timeout: int | float

The page timeout.

property title: str

The title of the current page.

title_contains(title: str, timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for checking that the title contains a case-sensitive substring.

title_is(title: str, timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for checking the title of a page.

unpin(script_key: ScriptKey) None

Remove a pinned script from storage.

property url: str

The URL of the current page.

url_changes(url: str, timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for checking the current url is different than a given string.

url_contains(url: str, timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for checking that the current url contains a case-sensitive substring.

url_is(url: str, timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for checking the current url.

url_matches(pattern: str, timeout: int | float | None = None, reraise: bool | None = None) bool

An expectation for checking the current url.

property wait: Wait

The current WebDriverWait instance.

waiting(timeout: int | float | None = None, ignored_exceptions: Type[Exception] | Iterable[Type[Exception]] | None = None) Wait

The final WebDriverWait instance.

property window_handles: list[str]

The handles of all windows within the current session.

class huskium.selenium.page.Page(driver: WD, timeout: int | float = 10, reraise: bool = True, remark: str = 'Page')

Bases: GenericPage[WebDriver, WebElement]

Module contents