I am trying to make my script perform a specific action based on the existence of a value in a table row on a website. E.g if x is in row 1 of table 'lab', create investigation, else move to next row and check if x is in that row. Sorry the website I am trying this on is not accessible by those who do not have an account but please see a simpler version of my code to help figure this out. As of now, I am stuck on the second for loop, the code below goes through each row and prints it out but just hangs. Might just be a break I am needing but I have tried it all (break, continue, pass).
#for each patient id in list, find the section in the patients web-table by looking through each row where the covid name and result is stored
#search for results table within a table in a web page for eicrs
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
continue #continue loop through rows
continue #not sure if needed
break #break out of topmost for loop and move to next line of code once value has been found to match condition.
print('done with that')
If I understand correctly what you need, you should remove both continue
from your code and the break
at the bottom as well and add a break
inside the if
and else
blocks so if you found the condition you are looking for and performed the action you need - now break the for loop.
Like this:
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('\n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
break
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
break
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
has been found to match condition.
print('done with that')
But maybe I do not understand your logic and the question