Last modified: Dec 01, 2023 By Alexander Williams

Python Selenium: Check if Element Exists - Examples

Example 1: Check if Element Exists by ID


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Check if an element with the specified ID exists
element_id = 'example-element'
if driver.find_elements_by_id(element_id):
    print(f"Element with ID '{element_id}' exists.")
else:
    print(f"Element with ID '{element_id}' does not exist.")

# Close the browser
driver.quit()

Output:


# Element with ID 'example-element' exists.

Example 2: Check if Element Exists by XPath


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Check if an element with the specified XPath exists
element_xpath = '//div[@class="example-class"]'
if driver.find_elements_by_xpath(element_xpath):
    print(f"Element with XPath '{element_xpath}' exists.")
else:
    print(f"Element with XPath '{element_xpath}' does not exist.")

# Close the browser
driver.quit()

Output:


# Element with XPath '//div[@class="example-class"]' exists.

Example 3: Check if Element Exists by CSS Selector


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Check if an element with the specified CSS selector exists
element_css_selector = 'div.example-class'
if driver.find_elements_by_css_selector(element_css_selector):
    print(f"Element with CSS Selector '{element_css_selector}' exists.")
else:
    print(f"Element with CSS Selector '{element_css_selector}' does not exist.")

# Close the browser
driver.quit()

Output:


# Element with CSS Selector 'div.example-class' exists.

Example 4: Check if Element Exists by Link Text


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Check if an element with the specified link text exists
link_text = 'Example Link'
if driver.find_elements_by_link_text(link_text):
    print(f"Element with Link Text '{link_text}' exists.")
else:
    print(f"Element with Link Text '{link_text}' does not exist.")

# Close the browser
driver.quit()

Output:


# Element with Link Text 'Example Link' exists.

Example 5: Check if Element Exists by Partial Link Text


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Check if an element with the specified partial link text exists
partial_link_text = 'Example'
if driver.find_elements_by_partial_link_text(partial_link_text):
    print(f"Element with Partial Link Text '{partial_link_text}' exists.")
else:
    print(f"Element with Partial Link Text '{partial_link_text}' does not exist.")

# Close the browser
driver.quit()

Output:


# Element with Partial Link Text 'Example' exists.

Example 6: Check if Element Exists by Class Name


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Check if an element with the specified class name exists
class_name = 'example-class'
if driver.find_elements_by_class_name(class_name):
    print(f"Element with Class Name '{class_name}' exists.")
else:
    print(f"Element with Class Name '{class_name}' does not exist.")

# Close the browser
driver.quit()

Output:


# Element with Class Name 'example-class' exists.