I am trying to find some elements in a website, if find I have to print check_1 or check_2 or if not, print none. But I am stuck at if else condition as if-else condition is not working. my code:
try:
if driver.find_element_by_css_selector('div.hero__media'):
sheet.cell(row=i, column=4).value = 'grid'
print('grid')
elif driver.find_elements_by_css_selector('div.flexigrid--no-sublinks.flexigrid--4-2up'):
sheet.cell(row=i, column=4).value = 'banner'
print('banner')
else:
raise Exception('This is the exception you expect to handle')
except Exception as error:
sheet.cell(row=i, column=4).value = 'none'
print('none')
Last else is throwing manual exception so if I can't find the element it will go to except. Edit 1: I tried and changed the condition from if to if else now again the 2nd condition is not working, something is wrong with if else. Edit1_Code:
if driver.find_element_by_css_selector('div.flexigrid--no-sublinks.flexigrid--4-2up'):
sheet.cell(row=i, column=4).value = 'grid'
print('grid')
elif driver.find_elements_by_css_selector('div.hero__media'):
sheet.cell(row=i, column=4).value = 'banner'
print('banner')
else:
raise Exception('This is the exception you expect to handle')
except Exception as error:
sheet.cell(row=i, column=4).value = 'none'
print('none')```