Check if specific div element exists in Selenium


Selenium Python - Check if specific div element exists

In this tutorial, you will learn how to check if a div element with a specific locator (id, classname, etc.) exists in the webpage using Selenium in Python.

To check if a div element exists in Selenium Python, find the div element using a locator like id, class, XPath expression, etc., with find_element() method. If the method returns an element, then the div element we are looking for exists, but if the find_element() method throws NoSuchElementException, then we can say that the div element does not exist.

In the following code snippet, we use try-except to check if the div element exists with the id='mydiv1'.

try:
    element = driver.find_element(By.XPATH, "//div[@id='mydiv1']")
    print("The div element exists.")
except NoSuchElementException:
    print("The div element does not exist.")

Make sure that in the program you import NoSuchElementException from selenium.common.exceptions.

Examples

In the following examples, we shall consider loading the HTML file at path /tmp/selenium/index-4.html . The contents of this HTML file is given below.

<html>
  <body>
    <div id="mydiv1">Apple</div>
    <div id="mydiv2">Banana</div>
    <div id="mydiv3">Cherry</div>
  </body>
</html>

1. The div element exists

In the following program, we initialize a webdriver, navigate to a specific URL, and check if a div element with id="mydiv1" exists in the document or not.

From the HTML file, we can say that the div element exists. The program must determine the same.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('/tmp/selenium/index-4.html')

# Check if div element exists
try:
    div_element = driver.find_element(By.XPATH, "//div[@id='mydiv1']")
    print("The div element exists.")
except NoSuchElementException:
    print("The div element does not exist.")

# Close the driver
driver.quit()

Output

Selenium Python Example - Check if specific div element exists

2. The div element does not exist

In this example, we are going to check if there is a div with the id="mydiv87".

Since, we know that there is no such element in the document, find_element() must throw NoSuchElementException.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('/tmp/selenium/index-4.html')

# Check if div element exists
try:
    div_element = driver.find_element(By.XPATH, "//div[@id='mydiv87']")
    print("The div element exists.")
except NoSuchElementException:
    print("The div element does not exist.")

# Close the driver
driver.quit()

Output

Selenium Python Example - Screenshot that the div we are looking for does not exist

Summary

In this Python Selenium tutorial, we have given instructions on how to check if a specific div element exists or not, with example programs.