droefs / HLISA

A reimplementation of the Selenium API, emulating human interactions
https://pypi.org/project/HLISA/
GNU General Public License v3.0
72 stars 8 forks source link

MoveTargetOutOfBoundsException #48

Open rspecker opened 9 months ago

rspecker commented 9 months ago

Issue Summary

The get_cursor_coordinates method encounters a MoveTargetOutOfBoundsException when trying to retrieve cursor coordinates. The current implementation uses string slicing to extract x and y coordinates, leading to potential issues when the error message structure changes. selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: Move target (30000, 30000) is out of bounds of viewport dimensions (1280, 595) Stacktrace:...

Description

The get_cursor_coordinates method attempts to extract cursor coordinates from a MoveTargetOutOfBoundsException error. However, the current implementation relies on string slicing, which may lead to errors if the error message format changes.

Steps to Reproduce

driver.get("https://www.wikipedia.org/")
text_field = driver.find_element(By.NAME, "search")
hlisa_action._hlisa_action.send_keys("some text....", text_field).perform()

Expected Results

The get_cursor_coordinates method should reliably extract x and y coordinates from the MoveTargetOutOfBoundsException error message, ensuring robustness against potential changes in the error message format.

Actual Results

The current implementation uses string slicing, which might break if the error message format changes.

Environment

Possible Solution

Proposed solution using regular expressions:

def get_cursor_coordinates(driver):
    MOVE_PIXELS = 30000
    try:
        ac = ActionChains(driver)
        ac.move_by_offset(MOVE_PIXELS, MOVE_PIXELS)
        ac.perform()
    except MoveTargetOutOfBoundsException as ex:
        match = re.search(r'.*\((\d+),\s*(\d+)\)', str(ex))
        x = match.group(1)
        y = match.group(2)
        try:
            x = int(x)
            y = int(y)
        except Exception as e:
            raise NoCursorCoordinatesException() # If the coordinates are not integers, something went wrong
        if x < 0 or y < 0:
            raise NoCursorCoordinatesException()
        return (x - MOVE_PIXELS, y - MOVE_PIXELS)
    raise NoCursorCoordinatesException() # If no exception occured, something went wrong