Reputation: 69
I skip some notification with this code.But when notification don't display this line code makes error.
'''
driver.find_element_by_class_name('aOOlW.HoLwm').click
'''
How can I use this code only when that element display??
Upvotes: 1
Views: 67
Reputation: 3541
Try use like this:
element_selector = 'aOOlW.HoLwm'
def check_exists_by(selector, by=By.CLASS_NAME):
try:
driver.find_element(by, selector)
except NoSuchElementException:
return False
return True
if check_exists_by(element_selector):
driver.find_element_by_class_name(element_selector).click()
of course, you should import all of the modules:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
and it work if you have correct selector
if you want to check is displayed, not only present in dom, there's opportunity to add is_displayd flag:
def check_exists_by(selector, by=By.CLASS_NAME, is_displayed=True):
try:
el = driver.find_element(by, selector)
except NoSuchElementException:
return False
if is_displayed:
return el.is_displayed()
return True
Upvotes: 2