def get_el(self, locator: tuple, base: WebElement=None) -> WebElement: """ Shortcut to get an element. """ if base is None: base = self.browser return base.find_element(*locator)
def clear_nested_element(self, parent: WebElement, locator: Tuple[str, str]): try: element = parent.find_element(*locator) self.driver.execute_script("return arguments[0].scrollIntoView();", element) element.clear() except TimeoutException: raise Exception("Element not found") except NoSuchElementException: raise Exception("Element not found")
def find_nested_element_by_locator_and_value(self, parent: WebElement, locator: str, value: str) -> WebElement: try: element = parent.find_element(locator, value) self.driver.execute_script("return arguments[0].scrollIntoView();", element) return element except TimeoutException: raise Exception("Element not found") except NoSuchElementException: raise Exception("Element not found")
def select_option_value_on_nested_element(self, parent: WebElement, locator: Tuple[str, str], value: str): try: element = parent.find_element(*locator) self.driver.execute_script("return arguments[0].scrollIntoView();", element) select = Select(element) select.select_by_value(value) except TimeoutException: raise Exception("Element not found") except NoSuchElementException: raise Exception("Element not found")
def validate_nested_element_text(self, parent: WebElement, locator: Tuple[str, str], value: str): try: WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located(locator)) element = parent.find_element(*locator) self.driver.execute_script("return arguments[0].scrollIntoView();", element) assert element.text == value except TimeoutException: raise Exception("Element not found") except NoSuchElementException: raise Exception("Element not found") except AssertionError: print(element.text + " vs. " + value) raise Exception()
def find_price_at(where: WebElement, what: Locator): """ Находит цену в указанном месте страницы сайта. *N.B.* Функция не поддерживает локали и предполагает, что точка это разделитель десятичных знаков и отсутствует разделитель разрядов :param where: Объект, в контексте которого мы ищем цену :param what: Локатор элемента, в котором хранится цена :return: float цену товара """ price_text = where.find_element(*what).text search_result = re.search(r"\d+.?\d*", price_text) return float(search_result.group(0))