def click_to_element(element: WebElement, sleep_after: float = 0.5): """Click the provided element and handle WebDriverException.""" try: element.click() time.sleep(sleep_after) except WebDriverException: pass
def get_detail_block(self, list_id: str) -> WebElement: self.wait_for_find_element_by_id(list_id) try: return self.browser.find_element_by_id( f'id_place_card_{self.last_place_id}') except NoSuchElementException: return WebElement(id_=None, parent=None)
def get_article_title(article: WebElement): """ Returns article's title from given article WebElement. :param article: WebElement :return: article's title: str """ return article.find_element_by_css_selector("div.post-container>h2>a").text
def test_web_element_not_subclassed(): """A registered subtype of WebElement should work with isinstance checks.""" class MyWebElement: def __init__(self, parent, id, _w3c=True): self.parent = parent self.id = id self._w3c = _w3c # Test that non registered class instance is not instance of Remote WebElement my_web_element = MyWebElement('parent', '1') assert not isinstance(my_web_element, WebElement) # Register the class as a subtype of WebElement WebElement.register(MyWebElement) my_registered_web_element = MyWebElement('parent', '2') assert isinstance(my_registered_web_element, WebElement)
def get_button_text(element: WebElement): """Get the text either via `value` attribute or using (inner) `text`. `value` attribute works for <input type="button"...> or <input type="submit". `text` works for <button>elements, e.g. <button>text</button>. """ button_text = element.get_attribute("value") or element.text return button_text.lower()
def get_post_reactions_lines_collumns(element: WebElement): """ :rtype: list of WebElement """ if check_exists_by_xpath(element, XPATH_PAGE_POST_REACTIONS_COLS): return element.find_elements_by_xpath(XPATH_PAGE_POST_REACTIONS_COLS)
def get_element_attribute(self, element: WebElement, attribute="textContent") -> str or None: """ Given an HTML element and its attribute name, return attributes content. Args: element: HTML element of Selenium WebElement type attribute: attribute name, defaults to textContent Returns: HTML element's attribute text value """ if hasattr(element, "get_attribute"): return element.get_attribute(attribute) else: return None
def open_article_in_new_tab(driver: WebDriver, article: WebElement, article_title: str): """ Opens article in new tab by performing mouse clicking on link with SHIFT key pressed. With new tab open switches focus to it. :param driver: WebDriver :param article: WebElement :param article_title: str :return: """ article_link = article.find_element_by_link_text(article_title) ActionChains(driver) \ .move_to_element(article_link) \ .key_down(Keys.SHIFT) \ .click(article_link) \ .key_up(Keys.SHIFT) \ .perform() article_window = driver.window_handles[1] driver.switch_to.window(article_window)
def _is_group_expanded(group: WebElement): return "expanded" in group.get_attribute("class")
def filter_(element: WebElement) -> bool: classes = element.get_attribute("class").split(" ") return class_ in classes
def filter_(element: WebElement) -> bool: return bool(element.get_attribute(attr) == value)
def get_html(element: WebElement): # Restituisce il contenuto HTML di un WebElement. return element.get_attribute('innerHTML')
def is_active(input_element: WebElement): """Check if we can interact with the given element.""" try: return is_displayed(input_element) and input_element.is_enabled() except WebDriverException: return False
def is_displayed(element: WebElement): """ Check if the specified element is being displayed. Return False also on exception.""" try: return element.is_displayed() except (StaleElementReferenceException, WebDriverException): return False