def _scroll_to_elem(driver: WebDriver, elem: WebElement, y_delta=-70, step=70, verbose=False, stop_if_visible=True): prev_y = -1 while True: elem_y = elem.location['y'] target_y = elem.location['y'] + y_delta cur_y = driver.execute_script('return window.pageYOffset') if verbose: print( f'scroll_to_elem: target_y: {target_y} elem.displayed: {elem.is_displayed()} ' f'cur_y: {cur_y}') if abs(target_y - cur_y) < 50: driver.execute_script(f"window.scrollTo(0, {target_y})") prev_y = cur_y break elif (cur_y == prev_y) and elem.is_displayed() and elem.is_enabled(): break else: direction = +1.0 if (target_y - cur_y) >= 0 else -1.0 next_y = int(cur_y + direction * step * random.lognormvariate(0, 0.2)) driver.execute_script(f"window.scrollTo(0, {next_y})") prev_y = cur_y if should_stop(): break _human_wait(0.05)
def click(element: WebElement = None, element_parent: Union[None, WebDriver, WebElement] = None, locator: Locator = None, delay: Union[int, float] = None): """ Clicks on element. Repeats when it fails It is possible to provide element object directly or its parent object and locator. :param element: WebElement - if present, directly click on this WebElement :param element_parent: WebElement - if present first find the element to click on under this element/WebDriver object using locator parameter :param locator: locator tuple i.e. (By.CSS_SELECTOR, 'div.my-class') :param delay: optional delay between attempts """ for i in range(3, -1, -1): if locator and element_parent: try: element = element_parent.find_element(*locator) except WebDriverException as e: if i == 0: raise print("ERROR: cannot find element", e.args) sleep(0.5) continue if not element.is_displayed(): if i == 0: raise Exception('Element is not displayed') else: print('ERROR: element is not displayed') sleep(0.5) continue if not element.is_enabled(): if i == 0: raise Exception('Element is not enabled') else: print('ERROR: element is not enabled') sleep(0.5) continue try: sleep(0.5) element.click() if delay is not None: sleep(delay) break except WebDriverException as e: if i == 0: raise print("ERROR: cannot click on element", e.args)
def scrollAndScreenshotElement(_driver: webdriver, _element: WebElement, _id): if _element.is_enabled(): imgutils.scroll_to_top_of_page_with_JS(_driver) y_scroll_value = _element.location.get('y') imgutils.scroll_to_Coordinate_Y(_driver, y_scroll_value - 30) file_path = r'.\\screenshots\\' #print("Saving screenshot to: " + file_path + "screenshot_" + str(_id) + ".png") _driver.save_screenshot(file_path + "screenshot_" + str(_id) + ".png") imgutils.unhighlight_element(_driver, _element) return (file_path + "screenshot_" + str(_id) + ".png") # TODO: imprint the image location on the file '''
def _enabled(_el: WebElement): enab = _el.is_enabled() disab = _el.get_attribute("disabled") or _el.get_attribute("aria-disabled") return enab and not disab
def check_if_element_is_disabled(web_element: WebElement, element_name: str): """Check if provided web element is disabled.""" with assertion_msg( f"Expected '{element_name}' element to be disabled but it's not"): assert not web_element.is_enabled() logging.debug(f"As expected '{element_name}' field is disabled.")
def wait_enabled(element: WebElement): while not element.is_enabled(): sleep(1)
def __is_valid_element(element: WebElement): assert (element is not None) assert element.is_displayed() is True, "element is not displayed on the page" assert element.is_enabled() is True, "element is not enabled"
def is_element_interactible(element: WebElement) -> bool: return element.is_displayed() and element.is_enabled()