def until_not(self, method, message=''): """ Wait until the method returns a False value. * The method uses Selenium WebDriver Args: method: The condition to wait for message: The message to show if the condition is not met in the time frame. Returns: If the value is a webelement, return a Pylenium Element object If the value is a list of WebElement, return a Pylenium Elements object Else return the non-False value Examples: # wait until the title is not 'Home Page' py.wait().until_not(lambda x: x.title == 'Home Page' ) """ value = self._wait.until_not(method, message) if isinstance(value, WebElement): return Element(self._py, value, None) if isinstance(value, list): try: return Elements(self._py, value, None) except: pass # not a list of WebElement return value
def until(self, method, message=''): """ Wait until the method returns a non-False value. * The method uses Selenium WebDriver Args: method: The condition to wait for message: The message to show if the condition is not met in the time frame. Returns: If the value is a webelement, return a Pylenium Element object If the value is a list of WebElement, return a Pylenium Elements object Else return the non-False value Examples: # return an Element py.wait().until(lambda x: x.find_element_by_id('foo'), 'element "foo" was not found') # return Elements py.wait().until(lambda x: x.find_elements_by_xpath('//a')) # return True py.wait(5).until(lambda x: x.title == 'QA at the Point') """ value = self._wait.until(method, message) if isinstance(value, WebElement): return Element(self._py, value, None) if isinstance(value, list): try: return Elements(self._py, value, None) except: pass # not a list of WebElement return value
def findx(self, xpath: str, timeout: int = None) -> Elements: """ Finds the DOM elements that match the `xpath` selector. * If timeout=None (default), use the default wait_time. * If timeout > 0, override the default wait_time. * If timeout=0, poll the DOM immediately without any waiting. Args: xpath: The selector to use. timeout: The number of seconds to wait for this to succeed. Overrides the default wait_time. Returns: A list of the found elements. """ by = By.XPATH self.log.info( f'[STEP] py.findx() - Find elements with xpath: ``{xpath}``') try: if timeout == 0: elements = self.webdriver.find_elements(by, xpath) else: elements = self.wait(timeout).until( lambda x: x.find_elements(by, xpath), f'Could not find an element with xpath: ``{xpath}``') except TimeoutException: elements = [] return Elements(self, elements, locator=(by, xpath))
def find(self, css: str, timeout: int = None) -> Elements: """ Finds all DOM elements that match the `css` selector. * If timeout=None (default), use the default wait_time. * If timeout > 0, override the default wait_time. * If timeout=0, poll the DOM immediately without any waiting. Args: css: The selector to use. timeout: The number of seconds to wait for this to succeed. Overrides the default wait_time. Returns: A list of the found elements. """ by = By.CSS_SELECTOR self.log.info(f'[STEP] py.find() - Find elements with css: ``{css}``') try: if timeout == 0: elements = self.webdriver.find_element(by, css) else: elements = self.wait(timeout).until( lambda x: x.find_elements(by, css), f'Could not find any elements with the CSS ``{css}``') except TimeoutException: elements = [] return Elements(self, elements, locator=(by, css))
def xpath(self, xpath: str, at_least_one=True, timeout: int = 0) -> Union[Element, Elements]: """ Finds all DOM elements that match the `xpath` selector. Args: xpath: The selector to use. at_least_one: True if you want to make sure at least one element is found. False can return an empty list. timeout: The number of seconds to wait for this to succeed. Overrides the default wait_time. Returns: A list of the found elements. If only one is found, return that as Element. """ by = By.XPATH if at_least_one: self.log.step( f'py.xpath() - Find at least one element with xpath: ``{xpath}``' ) elements = self.wait(timeout).until( lambda x: x.find_elements(by, xpath), f'Could not find any elements with the CSS ``{xpath}``') else: self.log.step( f'py.xpath() - Find elements with xpath (no wait): ``{xpath}``' ) elements = self.webdriver.find_elements(by, xpath) if len(elements) == 1: self.log.info('Only 1 element matched your xpath') return Element(self, elements[0], locator=(by, xpath)) self.log.info(f'{len(elements)} elements matched your xpath') return Elements(self, elements, locator=(by, xpath))
def find(self, css: str, at_least_one=True, timeout: int = 0) -> Elements: """ Finds all DOM elements that match the `css` selector. Args: css: The selector to use. at_least_one: True if you want to make sure at least one element is found. False can return an empty list. timeout: The number of seconds to wait for this to succeed. Overrides the default wait_time. Returns: A list of the found elements. """ if at_least_one: self.log.step( f'py.find() - Find at least one element with css: ``{css}``') elements = self.wait(timeout).until( lambda _: self.webdriver.find_elements(By.CSS_SELECTOR, css), f'Could not find any elements with the CSS ``{css}``') else: self.log.action( f'py.find() - Find elements with css (no wait): ``{css}``') elements = self.webdriver.find_elements(By.CSS_SELECTOR, css) return Elements(self, elements)