Module pom_elements.multi_element
Expand source code
from typing import Optional, Tuple, Union
from pom_elements.base_element import BaseElement
from selenium import webdriver
from selenium.webdriver.common.by import By
class MultiElement(BaseElement):
"""Set any Page Element using a Selenium Locators.
Args:
webdriver (webdriver): A selenium webdriver instance.
timeout (float): An integer or float. Defaults to default timeout if not set.
css (str): the string represenation of a css selector. Uses By.CSS_SELECTOR.
id_ (str): the html id of the page element. Uses By.ID.
class_name (str): the html class of the page element. Uses By.NAME.
link_text (str): the texts of provided link. Uses By.LINK_TEXT.
partial_link_text (str): the partial text of a link. Uses. By.PARTIAL_LINK_TEXT.
tag (str): the name of the name. Uses By.TAG_NAME.
xpath (str): the xpath query of the page element. Uses. By.XPATH.
Raises:
AttributeError: The MultiClass selector must have one locator type and query.
AttributeError: The MultiClass cannot have more then one locator type and query.
"""
LOCATOR = {
"css": By.CSS_SELECTOR,
"id_": By.ID,
"name": By.NAME,
"class_name": By.CLASS_NAME,
"link_text": By.LINK_TEXT,
"partial_link_text": By.PARTIAL_LINK_TEXT,
"tag": By.TAG_NAME,
"xpath": By.XPATH,
}
"""The dictionary containing the Selenium LOCATOR reference."""
def __init__(
self, webdriver: webdriver = None, timeout: Optional[float] = 0.5, **kwargs
) -> None:
locators = {k: v for k, v in kwargs.items() if k in self.LOCATOR}
if len(locators) == 0 or kwargs is None:
raise AttributeError("No attribute was set. Please set a Locator.")
if len(locators) > 1:
raise AttributeError(
"Two attributes set and this can only set a single Locator."
)
k, v = next(iter(locators.items()))
self._locator = (self.LOCATOR[k], v)
super().__init__(webdriver, timeout)
@property
def locator(self) -> Tuple[By, str]:
"""Return the locator set on the class."""
return self._locator
@locator.setter
def locator(self, locator: Union[Tuple[str, str], str]) -> None:
"""Set the locator for the class."""
raise NotImplementedError(
"Changing an Element's locator is not avilable at this time."
)
Classes
class MultiElement (webdriver:= None, timeout: Union[float, NoneType] = 0.5, **kwargs) -
Set any Page Element using a Selenium Locators.
Args
webdriver:webdriver- A selenium webdriver instance.
timeout:float- An integer or float. Defaults to default timeout if not set.
css:str- the string represenation of a css selector. Uses By.CSS_SELECTOR.
id_:str- the html id of the page element. Uses By.ID.
class_name:str- the html class of the page element. Uses By.NAME.
link_text:str- the texts of provided link. Uses By.LINK_TEXT.
partial_link_text:str- the partial text of a link. Uses. By.PARTIAL_LINK_TEXT.
tag:str- the name of the name. Uses By.TAG_NAME.
xpath:str- the xpath query of the page element. Uses. By.XPATH.
Raises
AttributeError- The MultiClass selector must have one locator type and query.
AttributeError- The MultiClass cannot have more then one locator type and query.
Expand source code
class MultiElement(BaseElement): """Set any Page Element using a Selenium Locators. Args: webdriver (webdriver): A selenium webdriver instance. timeout (float): An integer or float. Defaults to default timeout if not set. css (str): the string represenation of a css selector. Uses By.CSS_SELECTOR. id_ (str): the html id of the page element. Uses By.ID. class_name (str): the html class of the page element. Uses By.NAME. link_text (str): the texts of provided link. Uses By.LINK_TEXT. partial_link_text (str): the partial text of a link. Uses. By.PARTIAL_LINK_TEXT. tag (str): the name of the name. Uses By.TAG_NAME. xpath (str): the xpath query of the page element. Uses. By.XPATH. Raises: AttributeError: The MultiClass selector must have one locator type and query. AttributeError: The MultiClass cannot have more then one locator type and query. """ LOCATOR = { "css": By.CSS_SELECTOR, "id_": By.ID, "name": By.NAME, "class_name": By.CLASS_NAME, "link_text": By.LINK_TEXT, "partial_link_text": By.PARTIAL_LINK_TEXT, "tag": By.TAG_NAME, "xpath": By.XPATH, } """The dictionary containing the Selenium LOCATOR reference.""" def __init__( self, webdriver: webdriver = None, timeout: Optional[float] = 0.5, **kwargs ) -> None: locators = {k: v for k, v in kwargs.items() if k in self.LOCATOR} if len(locators) == 0 or kwargs is None: raise AttributeError("No attribute was set. Please set a Locator.") if len(locators) > 1: raise AttributeError( "Two attributes set and this can only set a single Locator." ) k, v = next(iter(locators.items())) self._locator = (self.LOCATOR[k], v) super().__init__(webdriver, timeout) @property def locator(self) -> Tuple[By, str]: """Return the locator set on the class.""" return self._locator @locator.setter def locator(self, locator: Union[Tuple[str, str], str]) -> None: """Set the locator for the class.""" raise NotImplementedError( "Changing an Element's locator is not avilable at this time." )Ancestors
- BaseElement
- abc.ABC
Class variables
var LOCATOR-
The dictionary containing the Selenium LOCATOR reference.
Instance variables
var default_timeout-
Inherited from:
BaseElement.default_timeoutGet the default timeout.
var locator : Tuple[selenium.webdriver.common.by.By, str]-
Return the locator set on the class.
Expand source code
@property def locator(self) -> Tuple[By, str]: """Return the locator set on the class.""" return self._locator var webdriver :-
Inherited from:
BaseElement.webdriverGet the Element's Selenium webdriver instance …
Methods
def can_be_clicked(self, timeout: Union[int, NoneType] = None) -> bool-
Inherited from:
BaseElement.can_be_clickedReturn true if the webelement can be clicked in the time (seconds) provided …
def click(self, timeout: Union[float, NoneType] = None)-
Inherited from:
BaseElement.clickClick the web element if it is available to be clicked.
def find(self, timeout: Union[float, NoneType] = None) -> selenium.webdriver.remote.webelement.WebElement-
Inherited from:
BaseElement.findReturns the defined Selenium WebElement in the provided timeout or raise an error …
def get_all_attributes(self) -> dict-
Inherited from:
BaseElement.get_all_attributesReturn a dictionary containing all of the attributes of an element.
def get_attribute(self, name)-
Inherited from:
BaseElement.get_attributeReturns the attribute of the name provided.
def get_property(self, value: str) -> Any-
Inherited from:
BaseElement.get_propertyGet the property of the element …
def is_displayed(self)-
Inherited from:
BaseElement.is_displayedReturns true if the Element is displayed else false.
def is_invisible(self, timeout: Union[int, NoneType] = None) -> bool-
Inherited from:
BaseElement.is_invisibleReturn true if the webelement is invisible on the page within the time (seconds) provided …
def is_visible(self, timeout: Union[int, NoneType] = None) -> bool-
Inherited from:
BaseElement.is_visibleReturn true if the webelement is visible on the page in the time (seconds) provided …