def getTextResults(driver, delay, plateString, parameters, frameName=None): #print("getTextResults: " + driver.current_url) # for debug text_regex = parameters['resultIndexParameters']['regex'] pattern = re.compile(text_regex) if parameters['outputLocator'] == None: # skip finding text return None while True: try: # short delay with retry loop resultElement = WebDriverWait(driver, 0.1).until( EC.presence_of_element_located(parameters['outputLocator'])) except TimeoutException: timeout('getTextResults: text not found, trying again...') #driver.switch_to_default_content() foundFrame = findAndSelectFrame(driver, delay, frameName) continue # why does this not find the text #try this with the locator using rEGEX, and why not found? no frame? pass in frame? try: elemText = resultElement.text except StaleElementReferenceException: print("getTextResults: stale text element") elemText = "" # reset the element text and continue if elemText == 'No Records returned': return 0 if elemText == 'Record 1 of 1': return elemText text = pattern.findall(elemText) if len(text): #print("getTextResults", text) # for debug return text[0] return None
def findElementOnPage(driver, delay, elementLocator, window=None): if elementLocator == None: # skip finding the element return None if window is not None: driver.switch_to_window(window) # switch to window if supplied print("findElementOnPage: switched to target window") try: element = WebDriverWait(driver, delay).until( EC.presence_of_element_located(elementLocator)) return element except TimeoutException: timeout('findElementOnPage: element' + str(elementLocator) + 'not found') return None
def newPageIsLoaded(driver, delay, currentElement): # depricated ? def isStale(self): if type(currentElement) == type( None): # when there is no element to check, return True # quit immediately #return False # loop until timeout occurs try: # poll the current element with an arbitrary call WebDriverWait(driver, delay).until(EC.staleness_of(currentElement)) return False except StaleElementReferenceException: return True try: WebDriverWait(driver, delay).until(isStale) return True except TimeoutException: timeout('newPageLoaded: old page reference never went stale') return False
def printableText(driver, delay, aString, ouputFileHandle): try: # delay to ignore presence of initial message WebDriverWait(driver,delay).until(EC.alert_is_present()) except: pass try: locator = (By.XPATH, '//p[@class="search-snippet"]') elems = WebDriverWait(driver, delay).until(EC.presence_of_all_elements_located(locator)) for element in elems: stringText = filter(lambda x: x in aString.printable, element.text) print stringText ouputFileHandle.write(stringText) except TimeoutException: timeout() driver.close()
def findTargetPage(driver, delay, locator, frameName=None): try: handle = driver.current_window_handle except NoSuchWindowException: print("findTargetPage: nothing to process, all windows finished?") return None handles = driver.window_handles for handle in handles: # test each window for target driver.switch_to_window(handle) foundFrame = findAndSelectFrame(driver, delay, frameName) print("findTargetPage: Searching for ", locator) # for debug purposes try: elems = WebDriverWait(driver, delay).until( EC.presence_of_all_elements_located(locator)) except TimeoutException: timeout('findTargetPage: locator element not found') continue #print("findTargetPage: found '", element.text, "'") # for debug purposes return handle print("findTargetPage: 'target page' not found") return None