def select_combo(self, element, option): """ Selects the option on the combobox. :param element: Combobox element :type element: Beautiful Soup object :param option: Option to be selected :type option: str Usage: >>> #Calling the method: >>> self.select_combo(element, "Chosen option") """ combo = Select(self.driver.find_element_by_xpath(xpath_soup(element))) value = next( iter( filter(lambda x: x.text[0:len(option)] == option, combo.options)), None) if value: time.sleep(1) text_value = value.text combo.select_by_visible_text(text_value) print(f"Selected value for combo is: {text_value}")
def filter_displayed_elements(self, elements, reverse=False): """ [Internal] Receives a BeautifulSoup element list and filters only the displayed elements. :param elements: BeautifulSoup element list :type elements: List of BeautifulSoup objects :param reverse: Boolean value if order should be reversed or not. - **Default:** False :type reverse: bool :return: List of filtered BeautifulSoup elements :rtype: List of BeautifulSoup objects Usage: >>> #Defining the element list: >>> soup = self.get_current_DOM() >>> elements = soup.select("div") >>> #Calling the method >>> self.filter_displayed_elements(elements, True) """ #1 - Create an enumerated list from the original elements indexed_elements = list(enumerate(elements)) #2 - Convert every element from the original list to selenium objects selenium_elements = list(map(lambda x : self.driver.find_element_by_xpath(xpath_soup(x)), elements)) #3 - Create an enumerated list from the selenium objects indexed_selenium_elements = list(enumerate(selenium_elements)) #4 - Filter elements based on "is_displayed()" and gets the filtered elements' enumeration filtered_selenium_indexes = list(map(lambda x: x[0], filter(lambda x: x[1].is_displayed(), indexed_selenium_elements))) #5 - Use List Comprehension to build a filtered list from the elements based on enumeration filtered_elements = [x[1] for x in indexed_elements if x[0] in filtered_selenium_indexes] #6 - Sort the result and return it return self.zindex_sort(filtered_elements, reverse)
def soup_to_selenium(self, soup_object): """ [Internal] An abstraction of the Selenium call to simplify the conversion of elements. :param soup_object: The BeautifulSoup object to be converted. :type soup_object: BeautifulSoup object :return: The object converted to a Selenium object. :rtype: Selenium object Usage: >>> # Calling the method: >>> selenium_obj = lambda: self.soup_to_selenium(bs_obj) """ return self.driver.find_element_by_xpath(xpath_soup(soup_object))
def element_exists(self, term, scrap_type=enum.ScrapType.TEXT, position=0, optional_term="", main_container=".tmodaldialog,.ui-dialog"): """ [Internal] Returns a boolean if element exists on the screen. :param term: The first term to use on a search of element :type term: str :param scrap_type: Type of element search. - **Default:** enum.ScrapType.TEXT :type scrap_type: enum.ScrapType :param position: Position which element is located. - **Default:** 0 :type position: int :param optional_term: Second term to use on a search of element. Used in MIXED search. - **Default:** "" (empty string) :type optional_term: str :return: True if element is present. False if element is not present. :rtype: bool Usage: >>> element_is_present = element_exists(term=".ui-dialog", scrap_type=enum.ScrapType.CSS_SELECTOR) >>> element_is_present = element_exists(term=".tmodaldialog.twidget", scrap_type=enum.ScrapType.CSS_SELECTOR, position=initial_layer+1) >>> element_is_present = element_exists(term=text, scrap_type=enum.ScrapType.MIXED, optional_term=".tsay") """ if self.config.debug_log: print( f"term={term}, scrap_type={scrap_type}, position={position}, optional_term={optional_term}" ) if scrap_type == enum.ScrapType.SCRIPT: return bool(self.driver.execute_script(term)) elif (scrap_type != enum.ScrapType.MIXED and scrap_type != enum.ScrapType.TEXT): selector = term if scrap_type == enum.ScrapType.CSS_SELECTOR: by = By.CSS_SELECTOR elif scrap_type == enum.ScrapType.XPATH: by = By.XPATH if scrap_type != enum.ScrapType.XPATH: soup = self.get_current_DOM() container_selector = self.base_container if (main_container is not None): container_selector = main_container containers = self.zindex_sort(soup.select(container_selector), reverse=True) container = next(iter(containers), None) if not container: return False try: container_element = self.driver.find_element_by_xpath( xpath_soup(container)) except: return False else: container_element = self.driver element_list = container_element.find_elements(by, selector) else: if scrap_type == enum.ScrapType.MIXED: selector = optional_term else: selector = "div" element_list = self.web_scrap(term=term, scrap_type=scrap_type, optional_term=optional_term, main_container=main_container) if position == 0: return len(element_list) > 0 else: return len(element_list) >= position
def input_value(self, field, value, ignore_case=True): """ [Internal] [returns Bool] Sets a value in an input element. Returns True if succeeded, False if it failed. """ success = False endtime = time.time() + 60 while (time.time() < endtime and not success): unmasked_value = self.remove_mask(value) print(f"Searching for: {field}") if re.match(r"\w+(_)", field): elements_list = self.web_scrap( f"[name*='{field}']", scrap_type=enum.ScrapType.CSS_SELECTOR) element = next( iter( filter( lambda x: re.search(f"{field}$", x.attrs["name"]), elements_list)), None) else: element = self.web_scrap(field, scrap_type=enum.ScrapType.TEXT, label=True) if not element: continue #preparing variables that would be used main_element = element #Aguardando campo ser carregado em tela #Waiting for field to be loaded in the screen element_first_children = next( (x for x in element.contents if x.name in ["input", "select", "textarea"]), None) if element_first_children is not None: main_element = element_first_children else: element_div = next( (x for x in element.contents if x.name in ["div"]), None) element_first_children = next( (x for x in element_div.contents if x.name in ["input", "select", "textarea"]), None) main_element = element_first_children try: input_field = lambda: self.driver.find_element_by_xpath( xpath_soup(main_element)) except: time.sleep(1) input_field = lambda: self.driver.find_element_by_xpath( xpath_soup(main_element)) valtype = "C" main_value = value try: if not input_field().is_enabled( ) or "disabled" in main_element.attrs: self.log_error( self.create_message(['', field], enum.MessageType.DISABLED)) except: time.sleep(1) if not input_field().is_enabled( ) or "disabled" in main_element.attrs: self.log_error( self.create_message(['', field], enum.MessageType.DISABLED)) cId = main_element.attrs['id'] self.tries = 1 self.wait_elements_load(cId, main_element.name) time.sleep(0.5) if main_element.name == 'select': self.SetComboBox(cId, main_value) success = True else: try: input_field().clear() self.send_keys(input_field(), main_value) self.send_keys(input_field(), Keys.TAB) current_value = self.get_web_value(input_field()).strip() if "fakepath" in current_value: success = True else: success = current_value == main_value except: continue if not success: self.log_error( f"Could not input value {value} in field {field}")