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 getx(self, xpath: str, timeout: int = None) -> Element: """ Finds the DOM element 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: The first element that is found, even if multiple elements match the query. """ by = By.XPATH self.log.info( f'[STEP] py.getx() - Find the element with xpath: ``{xpath}``') if timeout == 0: element = self.webdriver.find_element(by, xpath) else: element = self.wait(timeout).until( lambda x: x.find_element(by, xpath), f'Could not find an element with xpath: ``{xpath}``') return Element(self, element, locator=(by, xpath))
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 get(self, css: str, timeout: int = None) -> Element: """ Get the DOM element that matches 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: The first element that is found, even if multiple elements match the query. """ self.log.info( f'[STEP] py.get() - Find the element with css: ``{css}``') by = By.CSS_SELECTOR if timeout == 0: element = self.webdriver.find_element(by, css) else: element = self.wait(timeout).until( lambda x: x.find_element(by, css), f'Could not find element with the CSS ``{css}``') return Element(self, element, locator=(by, css))
def contains(self, text: str, timeout: int = None) -> Element: """ Get the DOM element containing the `text`. * 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: text: The text for the element to contain. timeout: The number of seconds to wait for this to succeed. Overrides the default wait_time. Returns: The first element that is found, even if multiple elements match the query. """ self.log.info( f'[STEP] py.contains() - Find the element with text: ``{text}``') locator = (By.XPATH, f'//*[contains(text(), "{text}")]') if timeout == 0: element = self.webdriver.find_element(*locator) else: element = self.wait(timeout).until( lambda x: x.find_element(*locator), f'Could not find element with the text ``{text}``') return Element(self, element, locator)
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 get(self, css: str, timeout: int = 0) -> Element: """ Get the DOM element that matches the `css` selector. Args: css: The selector to use. timeout: The number of seconds to wait for this to succeed. Overrides the default wait_time. Returns: The first element that is found, even if multiple elements match the query. """ self.log.step(f'py.get() - Find the element with css: ``{css}``') element = self.wait(timeout).until( lambda _: self._webdriver.find_element(By.CSS_SELECTOR, css), f'Could not find element with the CSS ``{css}``') return Element(self, element)
def contains(self, text: str, timeout: int = 0) -> Element: """ Get the DOM element containing the `text`. Args: text: The text for the element to contain. timeout: The number of seconds to wait for this to succeed. Overrides the default wait_time. Returns: The first element that is found, even if multiple elements match the query. """ self.log.step( f'py.contains() - Find the element with text: ``{text}``') locator = (By.XPATH, f'//*[contains(text(), "{text}")]') element = self.wait(timeout).until( lambda x: x.find_element(*locator), f'Could not find element with the text ``{text}``') return Element(self, element, locator)