I have a list of elements. Each element in the list has a string value in the following format:
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(87)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
I would like to extract only the numbers. The numbers always comes in brackets after Name[wi, It is always the same format.
How can i extract the numbers and store it in a variable so i can use it?
My Python code will iterate over the elements and if it finds a number above 86 it should return false, else return true.
My current Python code is:
def is_match_audit_code_displayed(self):
elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
for i in elements:
if "86" not in i.text:
print i.text
return True
return False
Thanks, Riaz
Using alecxe answer I have included it in my Python method. In the list of elements find the numeric value inside the brackets using regex. For each item in the list check if the number value is less than 86. If it is return true else return false.
Here is the full method routine:
def is_match_audit_code_less_than_max_value_displayed_for_the_filter_report_results(self, max_code):
try:
elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
pattern = re.compile(r"Name\[wi ,\((\d+)\)\]")
for element in elements:
value = pattern.findall(element.text)
if len(value) != 1:
return False
value = int(value[0])
if value > max_code: # e.g. max_code is 86
return False
return True
except NoSuchElementException, e:
print value
print "Element not found "
print e
screenshot_name = elements + value + get_datetime_now()
self.save_screenshot(screenshot_name)