def send_keys_to(driver: webdriver, identifier: str, element_type: By, keystrokes: str = '') -> None: """ :param driver: A webdriver object. :param identifier: element unique identifier. :param element_type: NAME, ID, XPATH, etc. :param keystrokes: String value to send to field. :return: None. """ # Checks to make sure we have something to send before executing if keystrokes != '' and keystrokes != 'None' and\ type(keystrokes) is not None: # Something needs to be sent, so we will try to send it try: # clear the field first # Warning: If your element contains an onblur, this can trigger it driver.find_element(element_type, identifier).clear() driver.find_element(element_type, identifier)\ .send_keys(keystrokes) except NoSuchElementException: pass # define errors and catch behavior here else: pass
def waitforelementbyxpath(self, driver: webdriver, xpath, seconds): print("Waiting for element") for i in range(seconds): try: driver.find_element(By.XPATH, xpath) break except NoSuchElementException: driver.implicitly_wait(1)
def waitforelementbytext(self, driver: webdriver, text, seconds): print("Waiting for element") for i in range(seconds): try: driver.find_element(By.LINK_TEXT, text) break except NoSuchElementException: driver.implicitly_wait(1)
def disabled_notifications(driver: webdriver, version) -> None: ''' This function disables the notifications on the FMC to prevent the notification popups from crashing the selenium. :param driver: The web browser driver. :param version: The version of FMC you are using. :return: None ''' if version == '6.2.3.13': tasks_icon = '/html/body/div[13]/div[1]/ul/li[12]/div/div[3]' gear_icon = '/html/body/div[13]/div[1]/ul/li[12]/div/div[4]/div[4]' notifications_icon = '/html/body/div[13]/div[1]/ul/li[12]/div/div[5]/ul/li/div/div/img' enabled_image = 'YgAAADuklEQVR42tWV7U9TZxiHnW7TLX' disabled_image = 'YgAAAC8UlEQVR42tWVXUuaYRjHKxmxsY' elif version == '6.3.0': tasks_icon = '/html/body/div[7]/div[2]/div/div[2]/div/ul/li[8]/div/div[3]' gear_icon = '/html/body/div[7]/div[2]/div/div[2]/div/ul/li[8]/div/div[4]/div[4]' notifications_icon = '/html/body/div[7]/div[2]/div/div[2]/div/ul/li[8]/div/div[5]/ul/li/div/div/img' enabled_image = 'YgAAADuklEQVR42tWV7U9TZxiHnW7TLX' disabled_image = 'YgAAAC8UlEQVR42tWVXUuaYRjHKxmxsY' else: tasks_icon = '' gear_icon = '' notifications_icon = '' enabled_image = '' disabled_image = '' time.sleep(2) WebDriverWait(driver, 120).until( expected_conditions.presence_of_element_located((By.XPATH, tasks_icon)) ) # Waits until it finds the tasks icon on the upper right corner and timeout in 120 seconds. tasks_element = driver.find_element(By.XPATH, tasks_icon) tasks_element.click() gear_element = driver.find_element(By.XPATH, gear_icon) gear_element.click() notifications_button = driver.find_element(By.XPATH, notifications_icon) notifications_button_img = notifications_button.get_attribute('src')[64:96] # This are the enabled and disabled images for notifications. # In earlier versions or newer versions of the Cisco Management Center this icons might or may not be different. if notifications_button_img == enabled_image: print('Disabling notifications!') print(notifications_button_img) notifications_button.click() elif notifications_button_img == disabled_image: print('Button is already disabled!') print(notifications_button_img) tasks_element.click() time.sleep(2)
def wait_for_javascript_load(driver: webdriver, action, element_type: By, identifier: str, wait: int = 10) -> None: """ Takes in an element ref to check for staleness to determine DOM modification or reload. :param driver: webdriver. :param action: action that causes reload of DOM or firing of JS script. :param element_type: By type. :param identifier: element id. :param wait: time before timeout exception. :return: None. """ old_ref = driver.find_element(element_type, identifier) action # any drivver call such # as driver.find_element(By.ID, 'saveButton').click() sleep(.5) # hopefully avoid race condition print('beginning wait for page reload') try: # wait until old Selenium Object Reference is invalid WebDriverWait(driver, wait).until(ec.staleness_of(old_ref)) except NoSuchElementException: pass print('Page has completed reload')
def _get_element(self, driver: webdriver, reference=None): """ return the specified element :param driver: Selenium Driver :param reference: (optional) Identifier of the specific item (where the element is not enough) :return: Selenium Element """ try: return driver.find_element(self.selector_type, self.selector_value) except NoSuchElementException as e: self.raise_not_found()
def generic_click_helper(driver: webdriver, identifier: str, element_type: By) -> None: """ A generic click on a desired element. Useful for onblurs or selecting elements. :param driver: webdriver object. :param identifier: unique element id. :param element_type: XPATH, NAME, ID, etc. :return: None. """ try: element = driver.find_element(element_type, identifier) driver.execute_script('arguments[0].click()', element) except NoSuchElementException: pass
def check_subscription_popup(driver: webdriver): # Check if there is a subscription popup, then close it. try: WebDriverWait(driver, 5).until( EC.visibility_of_element_located( (By.XPATH, '//*[@id="popover-email-div"]'))) WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="popover-x"]'))) close_alert_button = driver.find_element(By.XPATH, '//*[@id="popover-x"]') webdriver.ActionChains(driver).move_to_element( close_alert_button).perform() close_alert_button.click() except TimeoutException: # If there is no pop-up, do nothing and continue the flow. pass
def get_dict_qle(driver: webdriver) -> Dict[str, QuestionLabelElements]: q_element_labels: List[FirefoxWebElement] = driver.find_elements( By.XPATH, IndeedConstants.XPath.ALL_QUESTION_LABELS) q_element_inputs: List[FirefoxWebElement] = driver.find_elements( By.XPATH, IndeedConstants.XPath.ALL_QUESTION_INPUTS) dict_qle: Dict[str, QuestionLabelElements] = OrderedDict() for element_input in q_element_inputs: element_name = element_input.get_attribute( HTMLConstants.Attributes.NAME) if element_input.get_attribute( HTMLConstants.Attributes.TYPE ) == HTMLConstants.InputTypes.HIDDEN: pass else: if dict_qle.get(element_name, None) is None: qle = QuestionLabelElements() qle.add_element(element_input) qle.question.name = element_name dict_qle[element_name] = qle else: dict_qle[element_name].add_element(element_input) # Match Labels to Inputs for element_label in q_element_labels: label_for = element_label.get_attribute( HTMLConstants.Attributes.FOR) xpath_element_name = IndeedConstants.XPath.compute_xpath_input_name_of_label( label_for) element_name = driver.find_element( By.XPATH, xpath_element_name).get_attribute( HTMLConstants.Attributes.NAME) label_text = element_label.get_attribute( HTMLConstants.Attributes.INNER_TEXT) if dict_qle.get(element_name, None) is None: print('No input for label ' + label_text) else: dict_qle[element_name].label = label_text for key, qle in dict_qle.items(): qle.compute_question(driver) return dict_qle
def dropdown_select_helper(driver: webdriver, by: By, identifier: str, value: str) -> None: """ :param driver: webdriver object. :param by: By type. :param identifier: identifier for element. :param value: text value to search dropdown for. :return: None. """ # make sure we're looking for something if value != '': try: Select(driver.find_element(by, identifier))\ .select_by_visible_text(value) except NoSuchElementException: pass # Define errors and behavior here else: pass
def radio_button_helper(driver: webdriver, common_id: str, unique_id: str) -> None: """ Helper to deal with radio buttons which share NAME, ID attributes. :param driver: webdriver object. :param common_id: example: "@name='my_radio_button'" :param unique_id: example: "@value='01'" :return: None. """ # build an xpath to locate element on page, execute through JS engine try: # Builds and executes the xpath we need. # Who said they were inflexible? driver.execute_script( 'arguments[0].click()', driver.find_element( By.XPATH, ''.join(['//*[', common_id, ' and ', unique_id, ']']))) except NoSuchElementException: pass
def check_box_helper(driver: webdriver, identifier: str, element_type: By, check: str) -> None: """ Helper method to check a checkbox. This is useful as shorthand, and if a box is obscured as JavaScript will click under the obscuring element; Python binding would not. :param driver: webdriver object. :param identifier: unique identifier for your element. :param element_type: XPATH, NAME, ID, etc :param check: string that decides to either check or not check element. :return: """ if check.lower() == 'check': try: # Find checkbox # expose Selenium object reference to JavaScript engine checkbox = driver.find_element(element_type, identifier) driver.execute_script('arguments[0].click()', checkbox) except NoSuchElementException: pass # Define errors, catch behavior here else: pass