def delete(self): """Deletes the file and returns if it was able to delete the file.""" if not self.created() and self.doc.closed: try: write("Deleting File...") remove(self.get_full_path()) return True except OSError: warning("Unable to Delete file: ", self.get_full_path()) return False
def wait_until(self, selector): """Tells the browser to wait for at the most 20 seconds before telling the page to stop loading by if the css selector is on the page. Args: selector: CSS Selector string.""" try: self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, selector))) self.browser.execute_script('window.stop();') except TimeoutException: pyprint.warning(selector, "TIMEOUT EXCEPTION")
def write_list(self, message) -> bool: if self.created() and self.doc.closed: try: write("Writing to File...") self.doc = open(self.get_full_path(), 'w') for line in message: self.doc.write(line + '\n') self.doc.close() return True except OSError: warning("Could not find or open file ", self.get_full_path()) return False
def read(self) -> list: """Reads text from the file and returns array of strings. If unable to open file will print to user and return None""" if self.created(): try: write("Reading File...") lines = [] self.doc = open(self.get_full_path(), 'r') for line in self.doc: lines.append(line.split('\n')[0]) self.doc.close() return lines except OSError: warning("Could not find or open ", self.get_full_path()) return []
def get_elements(self, selector, element=False) -> [WebElement]: """Finds elements in the browser using a css selector then returns those WebElements. If given a WebElement will then css search in that element. If elements are not found on the page will return empty list. Args: selector: CSS String element: WebElement DEFAULT: False""" try: self.wait_until(selector) if element: return element.find_elements_by_css_selector(selector) else: return self.browser.find_elements_by_css_selector(selector) except NoSuchElementException: pyprint.warning(selector, warn="ELEMENTS NOT FOUND") return []
def get_element(self, selector, element=False) -> WebElement: """Finds element in the browser using a CSS selector then returns that WebElement. If given a WebElement will then css search in that element. If element is not found on the page will return false. Args: selector: CSS String element: WebElement DEFAULT: False""" try: self.wait_until(selector) if element: return element.find_element_by_css_selector(selector) else: return self.browser.find_element_by_css_selector(selector) except NoSuchElementException: pyprint.warning(selector, warn="ELEMENT NOT FOUND") return False
def write(self, *messages) -> bool: """Opens and writes given data to the file as text. Will print to the user if were unable to write to the file and return if we were able to write to file. Args: messages: Takes in multiple objects and strings.""" if self.created() and self.doc.closed: try: write("Saving File...") self.doc = open(self.get_full_path(), 'w') for line in list(messages): self.doc.write(str(line) + '\n') self.doc.close() return True except OSError: warning("Could not find or open file ", self.get_full_path()) return False
def send_text(self, text: str, selector) -> bool: """Sends text to a specific element. Selector can either be CSS string or it can be an element from selenium. Returns bool if was able to write to element or not. Args: text: Text that will be sent to the element. selector: Either CSS Selector or Element Object.""" try: if type(selector) is str: if self.check_selector(selector): selector = self.browser.find_element_by_css_selector(selector) selector.send_keys(text) return True else: selector.send_keys(text) return True except ElementNotInteractableException: pyprint.warning(selector, warn='SEND TEXT FAILURE') return False
def create(self, filename="", ext="ini", location=""): """Tries to create the file and the returns if the file has been created or not. if Attributes are given it will open or try to create a new file Attributes: filename: Filename to create without extension. ext: The type of text file being created. Will Default to ini location: The location on HDD to look ie C://... Will Default to location where program is running.""" if filename: self.filename = filename self.ext = ext self.location = location try: write("Creating File...") self.doc = open(self.get_full_path(), "a") self.doc.close() return True except OSError: warning("Could not open: ", self.get_full_path()) return False
def click(self, selector, text="", sleep_time=2) -> bool: """Clicks the given object. Selector can either be CSS string or it can be an element from selenium. At the end after the click the program will pause the amount of sleep_time. Returns boolean if program was able to click element or not. Args: selector: Either CSS Selector or Element Object. sleep_time: Time in seconds. DEFAULT: 2 text: Text to search for. DEFAULT: blank string""" try: if type(selector) is str: # If selector is a CSS string if text: element = self.get_element_by_text(selector, text) if element: element.click() sleep(sleep_time) else: pyprint.warning(selector, warn='ELEMENT NOT FOUND') else: element = self.get_element(selector) if element and text: element.click() sleep(sleep_time) return True else: # If get_element didn't find the element element = self.get_element_by_text(selector, text) if element: element.click() sleep(sleep_time) elif type(selector) is bool: pyprint.warning(selector, warn='CLICK FAILURE') return False else: # If selector is WebElement Object selector.click() sleep(sleep_time) return True except ElementClickInterceptedException: pyprint.warning(selector, warn='CLICK INTERCEPTED FAILURE') return False except ElementNotInteractableException: pyprint.warning(selector, warn='CLICK INTERACTABLE FAILURE')