How to tell Selenium to always look for specific element [PYTHON]

Viewed 39

I'm running a Selenium script on a website where, randomly, a small notification pops up that blocks some other elements on the page. I was wondering if there was a way to tell Selenium to always be on the look out for this specific element? Once it spots it, I'd make it so that it closes the notification (by clicking a button that is found on the element). Any help would be greatly appreciated – thank you.

The Full XPATH of the element I'm looking out for:

/html/body/div[4]/div/div/snack-bar-container/div/div/bs-custom-snackbar/div/div

The Full XPATH of the button that I want to click (on the pop-up element):

/div/snack-bar-container/div/div/bs-custom-snackbar/div/button
1 Answers

You can just add a try except when clicking on the element that it blocks. If it tries and fails then the popup must be there and then you can click the button that closes it.

try:
   driver.find_element_by_xpath(ELEMENT BELOW POPUP XPATH).click()
except:
   driver.find_element_by_xpath(CLOSE BUTTON XPATH).click()
Related