Sometimes the element does not appear at once, for this case we need to use explicit wait:
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 5)
def is_element_exist(text):
try:
wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, text)))
except TimeoutException:
return False
Solution without try/ except
:
def is_element_exist(text):
elements = wait.until(EC.presence_of_all_elements_located((By.PARTIAL_LINK_TEXT, text)))
return None if elements else False
How explicit wait works you can read here.
Imports:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC