0

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')```
  • can you change `driver.find_elements_by_css_selector` to `driver.find_element_by_css_selector` in `elif` condition and try – NarendraR Mar 15 '19 at 13:45
  • @NarendraR tried still not working. It is not taking if else condition – Shashank Sahu Mar 15 '19 at 14:29
  • You mean your are checking whether checkbox present on webpage ? – NarendraR Mar 15 '19 at 14:31
  • `button =driver.find_element_by_css_selector('div.hero__media') //To check button is present on page if (len(button)>0):` – NarendraR Mar 15 '19 at 14:32
  • The issue here is my if else is not working, So whatever I put in if else, it is not working. I switched if condition with if else condition and if else condition was working – Shashank Sahu Mar 15 '19 at 14:42

2 Answers2

0

Try length count and check if it works.

if len(driver.find_elements_by_css_selector('div.hero__media'))>0:
            sheet.cell(row=i, column=4).value = 'grid'
            print('grid')

        elif len(driver.find_elements_by_css_selector('div.flexigrid--no-sublinks.flexigrid--4-2up'))>0:
            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.
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

You can use find_elements, get size and check if it not 0:

value = 'none'
if len(driver.find_elements_by_css_selector('div.hero__media')) > 0:
    value = 'grid'

elif len(driver.find_elements_by_css_selector('div.flexigrid--no-sublinks.flexigrid--4-2up')) > 0:
    value = 'banner'

print(value)
sheet.cell(row=i, column=4).value = value

Problem in your code is if first element not exist Selenium throw NoSuchElementException and skip other code. Fall to except part and never go to elif.

You can also check here how to check if element exist.

Sers
  • 12,047
  • 2
  • 12
  • 31
  • Why should I check the length? I just want to know if the element (of 'CSS selector') is present on a website or not – Shashank Sahu Mar 15 '19 at 14:32
  • Checking length of find elements give you how many elements exist in the DOM, 0 means not exist. – Sers Mar 15 '19 at 15:00