def wait_for_by_id_then_click(driver, element_id, wait_for_seconds=30): """ wait_for_by_id_then_click Waits for an element by id to be clickable and clicks it :param driver: Webdriver controller for the web page :param element_id: ID of the element to interact with. :param wait_for_seconds: Timeout value for the element to be clickable """ Log.info( f"Waiting {wait_for_seconds} seconds to be clickable, then clicking element with ID {element_id}" ) WebDriverWait(driver, wait_for_seconds).until( expected_conditions.element_to_be_clickable((By.ID, element_id))) retries = int(wait_for_seconds / 10) wait_seconds = 10 attempt_number = 1 while attempt_number <= retries: Log.info( f"Trying to click element: Attempt number {attempt_number} of {retries}" ) try: click_element_by_id(driver, element_id) return except Exception as e: Log.warning( f"Failed the click. Waiting {wait_seconds} seconds to retry. {e}" ) sleep(wait_seconds) attempt_number += 1 driver.save_screenshot("screenshot.png") driver.quit() raise FileNotFoundError("Out of retries. Could not click the element")
def get_rows(self, sql, *parameters, **kwparameters): rows = None try: rows = self._db.query(sql, *parameters, **kwparameters) except Exception, e: Log.warning('Mysql get rows error(%s)(%s)', sql, str(e))
def get_one(self, sql, *parameters, **kwparameters): row = None try: row = self._db.get(sql, *parameters, **kwparameters) except Exception, e: Log.warning('Mysql get one error(%s)(%s)', sql, str(e))
def __reconnect(cls): if cls.__mc_conn is not None: try: cls.__mc_conn.disconnect_all() except Exception, e: Log.warning('memcache disconnect(%s)', str(e)) cls.__mc_conn = None
def update(self, sql, *parameters, **kwparameters): ret = [False, 0] try: ret[1] = self._db.execute_rowcount(sql, *parameters, **kwparameters) ret[0] = True except Exception, e: Log.warning('Mysql update error(%s)(%s)', sql, str(e))
def delete(cls, key): try: cls.__mc_conn.delete(key) except Exception, e: Log.warning('memcache delete %s failed(%s)', key, str(e)) cls.__reconnect() try: cls.__mc_conn.delete(key) except Exception, e1: Log.warning('memcache re-delete %s failed(%s)', key, str(e1))
def wait_for_element_not_to_be_clickable(driver, element_xpath_or_id, element_type='xpath', wait_interval=5, max_wait_time=60): """ wait_for_element_not_to_be_clickable Recursively checks for the element to not be clickable :param driver: Webdriver for the browser :param element_xpath_or_id: Xpath or ID of the element we want to not be clickable. Defined by element_type :param element_type: 'xpath' or 'id' to select the appropriate Webdriver call for find element :param wait_interval: Sleep interval between checks :param max_wait_time: Timeout for the element to not be clickable :return: """ time_waited = 0 while time_waited < max_wait_time: Log.info( f"Waiting {wait_interval} seconds for element with {element_type} {element_xpath_or_id}" f" to NOT be clickable") try: if element_type == 'xpath': find_by_xpath(driver, element_xpath_or_id) else: find_by_id(driver, element_xpath_or_id) Log.warning( f"Element is still clickable. Waiting another {wait_interval} seconds. Waited {time_waited} sofar" ) sleep(wait_interval) time_waited += wait_interval except Exception as e: Log.info(f"Element is now not clickable. Returning. {e}") return take_screenshot(driver) raise FileNotFoundError(f"Element still clickable after {max_wait_time}")
def get(cls, key): val = None try: val = cls.__mc_conn.get(key) except Exception, e: Log.warning('memcache get %s failed(%s)', key, str(e)) cls.__reconnect() try: val = cls.__mc_conn.get(key) except Exception, e1: val = None Log.warning('memcache re-get %s failed(%s)', key, str(e1))
def set(cls, key, val, time_out=0): ret = True try: cls.__mc_conn.set(key, val, time_out) except Exception, e: Log.warning('memcache set %s failed(%s)', key, str(e)) cls.__reconnect() try: cls.__mc_conn.set(key, val, time_out) except Exception, e1: ret = False Log.warning('memcache re-set %s failed(%s)', key, str(e1))