Aug-14-2019, 09:04 PM
(Aug-13-2019, 05:07 PM)graham23s Wrote: What i'm having trouble with is, how can i find out which of the "submit_success" flags was found?If you want to keep track of an intermediate value, using
any()
might not be the best, since the assumption is that it's a side-effect free operation. It doesn't have to be side-effect free, you just need to put a little effort into getting it to work.>>> class ValueFinder: ... def __init__(self, callback): ... self.callback = callback ... self.match = None ... self.match_found = False ... def __call__(self, test): ... if self.callback(test): ... self.match = test ... self.match_found = True ... return self.match_found ... >>> submit_success = "TEXT_1|TEXT_2|TEXT_3|etc".split("|") >>> page_source = '''one fish, TEXT_2 fish, red fish, blue fish''' >>> finder = ValueFinder(lambda x: x in page_source) >>> if any(finder(s) for s in submit_success): ... print(finder.match) ... TEXT_2Or, instead of using
any()
, you could roll your own:>>> submit_success = "TEXT_1|TEXT_2|TEXT_3|etc".split("|") >>> page_source = '''one fish, TEXT_2 fish, red fish, blue fish''' >>> def first(test, seq): ... for item in seq: ... if test(item): ... return item ... return None ... >>> match = first(lambda s: s in page_source, submit_success) >>> if match: ... print(match) ... TEXT_2Once python 3.8 is out, you could also use the walrus operator to do this:
submit_success = "TEXT_1|TEXT_2|TEXT_3|etc" if any((match := s) in self.driver.page_source for s in submit_success.split('|')): # "match" is the TEXT_ value that matched