I want to write a function to check if some nav links exist or not. However, those nav links are under one of the listbox icons so I need to click the icon before and after to check if the element exists. The following code works. Nonetheless, is there a better way for writing this?
def func(locator, name):
if self.driver.find_element_by_xpath('{}{}/i'.format(locator, '[@aria-expand="false"]')):
self.driver.find_element_by_xpath(locator).click()
try:
est = True
self.driver.find_element_by_xpath('{}//a[text()="{}"]'.format(locator, name)).
except NoSuchElementException:
est = False
if self.driver.find_element_by_xpath('{}{}/i'.format(locator, '[@aria-expand="true"]')):
self.driver.find_element_by_xpath(locator).click()
return est