Interfaces¶
This section documents the core interfaces and base classes that form the foundation of the selenium package. These interfaces define the contract for actions and executors, providing a consistent API for extending functionality.
Base Classes¶
- class selenium_package.interfaces.base_action.BaseAction(web_instance: WebDriver | WebElement, web_element: WebElement | None = None)¶
Bases:
ABC- execute_action() Any¶
Execute the action by calling the _execute_action method. It will wrap the exception in a SeleniumBaseActionException.
- Returns:
Any: The result of the action.
- run() Any¶
Just a wrapper to execute the action and return the result.
- class selenium_package.interfaces.base_executor.BaseExecutor(action: BaseAction, web_element: WebElement = None, wait_to_verify_condition: int = None, timeout: int = 30)¶
Bases:
ABC- is_condition_to_stop_met(result: Any = None) bool¶
- run() Any¶
Run the executor. This method will execute the action until the condition to stop is met. Returns:
Any: the result of the action if the condition to stop is met.
- Raises:
TimeoutError: if the action took more than the timeout to complete.
Exceptions¶
The selenium package defines several custom exceptions that are used throughout the codebase:
- class selenium_package.interfaces.exceptions.exceptions.SeleniumBaseActionException(message: str)¶
Bases:
Exception
- class selenium_package.interfaces.exceptions.exceptions.SeleniumBaseGetterException(message: str)¶
Bases:
Exception
- class selenium_package.interfaces.exceptions.exceptions.MaximumAttemptsReachedException(message: str)¶
Bases:
Exception
- class selenium_package.interfaces.exceptions.exceptions.NoMorePagesException(message: str)¶
Bases:
Exception
Usage Examples¶
Here are some examples of how to use the base interfaces:
Creating a Custom Action:
from selenium_package.interfaces import BaseAction
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
class CustomAction(BaseAction):
def __init__(self, web_instance: WebDriver | WebElement, web_element: WebElement | None = None):
super().__init__(web_instance, web_element)
def _execute_action(self):
# Your custom action logic here
return "Action completed"
Creating a Custom Executor:
from selenium_package.interfaces import BaseExecutor, BaseAction
class CustomExecutor(BaseExecutor):
def __init__(self, action: BaseAction, web_element=None, wait_to_verify_condition=None, timeout=30):
super().__init__(action, web_element, wait_to_verify_condition, timeout)
def _is_condition_to_stop_met(self, result=None):
# Your custom condition logic here
return True # or False based on your condition
Handling Exceptions:
from selenium_package.interfaces import SeleniumBaseActionException
try:
action = CustomAction(webdriver)
result = action.execute_action()
except SeleniumBaseActionException as e:
print(f"Action failed: {e.message}")